2013-08-20 320 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

相關問題