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);
}
}
}
}
感謝您的幫助