2
我有一个woocommerce功能,可自动添加购物车免费礼物。有条件地添加或删除购物车中的免费产品
我想基于15
最低数量添加此我这个代码的作品,但是当我更新的车不要删除项目,如果数量不是15
当我更新购物车时,如果总计ID不同于15,我该如何自动删除产品?
}
//add_action('init', 'wcsg_add_product_to_cart');
add_action('wp_loaded', 'wcsg_add_product_to_cart', 99);
function wcsg_add_product_to_cart() {
if (! is_admin()) {
global $woocommerce;
//calcoliamo quanti prodotti ci sono nel carrello
$totalecarrello = $woocommerce->cart->cart_contents_count;
echo "<script type='text/javascript'>alert('$totalecarrello');</script>";
$cart = WC()->cart->get_cart();
$wcsgProduct = '0';
$wcsgProduct = get_option('wcsgProduct');
$product_id = $wcsgProduct;
if ($product_id== '0' || $product_id == NULL) {
// do nothing
} else {
$found = false;
if (sizeof($cart) > 0) {
foreach ($cart as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->id == $product_id)
$found = true;
}
//controlliamo quanti prodotti ci sono
if (! $found && $totalecarrello == 15)
//se sono 15 aggiungiamo il prodotto free
WC()->cart->add_to_cart($product_id);
$message = $woocommerce->cart->cart_contents_count;
echo "<script type='text/javascript'>alert('$message');</script>";
$totalecarrello = $woocommerce->cart->cart_contents_count;
//altrimenti no
} else {
//WC()->cart->add_to_cart($product_id);
WC()->cart->remove_cart_item($product_id);
}
}
}
}
感谢您的帮助