2013-08-20 266 views
1

我试图在我的WooCommerce主题中实现自定义增值销售功能。 当用户添加产品时,我需要将其他产品(upsell)添加到购物车。这意味着我必须为产品添加一个额外的参数,以便在产品从购物车中删除时,我可以获得关系产品/加售并删除关联的加售。WooCommerce在产品添加时将增加销售产品添加到购物车

用户可以通过复选框列表选择这些加售产品。这里是我的“内容单一product.php” WooCommerce模板的概述:

<form action="<?php echo esc_url($product->add_to_cart_url()); ?>" class="cart" method="post" enctype="multipart/form-data"> 
    <?php 

    // Get Upsells Product ID: 
    $upsells = $product->get_upsells(); 

    if (sizeof($upsells) == 0) return; 

    $meta_query = $woocommerce->query->get_meta_query(); 

    // Get Upsells: 
    $args = array(
     'post_type'   => 'product', 
     'ignore_sticky_posts' => 1, 
     'no_found_rows'  => 1, 
     'posts_per_page'  => $posts_per_page, 
     'orderby'    => $orderby, 
     'post__in'   => $upsells, 
     'post__not_in'  => array($product->id) 
    ); 

    $query = new WP_Query($args); 

    if ($query->have_posts()): ?> 
     <ul> 
     <?php while ($query->have_posts()): $query->the_post(); ?> 
      <label for="upsell_<?php the_ID(); ?>"><input type="checkbox" name="upsells[]" id="upsell_<?php the_ID(); ?>" value="<?php the_ID(); ?>" /> <?php the_title(); ?></label> 
     <?php endwhile; ?> 
     </ul> 
    <?php endif; ?> 

    <?php wp_reset_postdata(); ?> 

    <button type="submit">Add to cart</button> 
</form> 

上面的代码已经被简化..

然后,这里是我加入的functions.php的代码让我加售添加到购物车,并添加一个额外的行相关产品:

/** 
* Add upsells as extra data for added product 
*/ 
function add_upsells_to_cart($cart_item_key) { 
    global $woocommerce; 

    if (empty($_REQUEST['upsells']) || ! is_array($_REQUEST['upsells'])) 
     return; 

    // Prevent loop 
    $upsells = $_REQUEST['upsells']; 
    unset($_REQUEST['upsells']); 

    // Add upsell_parent row (if not already existing) for the associated product 
    if (! $cart_item_data['upsells'] || ! is_array($cart_item_data['upsells'])) 
     $cart_item_data['upsells'] = array(); 

    // Append each upsells to product in cart 
    foreach($upsells as $upsell_id) { 
     $upsell_id = absint($upsell_id); 

     // Add extra parameter to the product which contain the upsells 
     $woocommerce->cart->cart_contents[ $cart_item_key ]['upsells'] = $upsell_id; 

     // Add upsell to cart 
     $woocommerce->cart->add_to_cart($upsell_id); 
    } 
} 
add_action('woocommerce_add_to_cart', 'add_upsells_to_cart'); 

追加销售添加到购物车不如预期,但额外的行似乎在WC东西被丢弃。

当我在上一个函数结束时执行print_r($woocommerce->cart->cart_contents[ $cart_item_key ]);时,可以看到额外的加售参数,但是当我从购物车页面显示购物车时,会从购物车中删除加载参数。

有什么想法?

感谢, E.

+0

对于其他人,[强制塞尔斯(http://www.woothemes.com/products/force-sells/)手柄正是这个。 – helgatheviking

回答

5

所以最后,我找到了解决我的问题。

当通过功能get_cart_from_session()class-wc-cart.php的功能加载购物车时,问题发生了。

当额外的钥匙被添加到购物车项目时,我们需要通过woocommerce_get_cart_item_from_session过滤器注入钥匙。

用户现在可以选择加售,这个加售会自动添加到购物车,而不是添加产品。如果主产品被删除,我还有一个功能允许我删除加售。

这里是我的我加入到我的functions.php最终代码:

/** 
* Add upsells as extra data for added product 
*/ 
function add_upsells_to_cart($cart_item_key) { 
    global $woocommerce; 

    if (empty($_REQUEST['upsells']) || ! is_array($_REQUEST['upsells'])) 
     return; 

    // Prevent loop 
    $upsells = $_REQUEST['upsells']; 
    unset($_REQUEST['upsells']); 

    // Append each upsells to product in cart 
    foreach($upsells as $upsell_id) { 
     $upsell_id = absint($upsell_id); 

     // Add upsell into cart and set upsell_of as extra key with parent product item id 
     $woocommerce->cart->add_to_cart($upsell_id, 1, '', '', array('upsell_of' => $cart_item_key)); 
    } 
} 
add_action('woocommerce_add_to_cart', 'add_upsells_to_cart', 1, 6); 


/** 
* Inject upsell_of extra key cart item key when cart is loaded from session 
*/ 
function get_cart_items_from_session($item, $values, $key) { 
    if (array_key_exists('upsell_of', $values)) 
     $item[ 'upsell_of' ] = $values['upsell_of']; 

    return $item; 
} 
add_filter('woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3); 


/** 
* Remove associated upsells if product removed from cart 
*/ 
function remove_upsells_from_cart($cart_item_key) { 
    global $woocommerce; 

    // Get cart 
    $cart = $woocommerce->cart->get_cart(); 

    // For each item in cart, if item is upsell of deleted product, delete it 
    foreach($cart as $upsell_key => $upsell) 
     if ($upsell['upsell_of'] == $cart_item_key) 
      unset($woocommerce->cart->cart_contents[ $upsell_key ]); 
} 
add_action('woocommerce_before_cart_item_quantity_zero', 'remove_upsells_from_cart'); 
+0

我正在开发类似的东西,这个问题和解答有帮助,谢谢!如果我建议,当您查询您的加售列表时,您应该考虑检查产品的可见性和可用性。看看“visibility_meta_query”和“stock_status_meta_query”(http://docs.woothemes.com/wc-apidocs/source-class-WC_Query.html#441-478),你可以通过$ woocommerce-> query和将它们添加到您的查询参数 – ggg

+0

该死的,我正在寻找这个好几天!谢谢! – passatgt

相关问题