2014-01-17 50 views

回答

10

試試這個,

//For removing all the items from the cart 
global $woocommerce; 
$woocommerce->cart->empty_cart(); 
$woocommerce->cart->add_to_cart($product_id,$qty); 

類文件是wp-content/plugins/woocommerce/classes/class-wc-cart.php

希望它有助於..

+0

永遠歡迎.. –

+0

嘿,我應該在哪裏把這個類-WC-cart.php? –

8

我有一個確切的解決方案這個。 請嘗試下面的代碼。

add_filter('woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3); 

function wdm_empty_cart($cart_item_data, $product_id, $variation_id) 
{ 

    global $woocommerce; 
    $woocommerce->cart->empty_cart(); 

    // Do nothing with the data and return 
    return $cart_item_data; 
} 

上面的過濾器在函數add_to_cart()中的class-wc-cart.php中定義。
http://docs.woothemes.com/wc-apidocs/source-class-WC_Cart.html#774-905
因此,當添加到購物車按鈕被按下時,它清空購物車,然後添加產品。

+0

稍微降低優先級會有幫助,以便我們的過濾器在空車之前在其他任何地方運行? –

相關問題