से एक फ्रीबी उत्पाद ऑटो को ऑटो या ऑटो हटाएं, मैं एक WooCommerce दुकान चला रहा हूं, जहां हम प्रत्येक ग्राहक को एक फ्रीबी (ई-बुक) देना चाहते हैं जो टोकरी में दिखाएगा, आपके जोड़े जाने के बाद टोकरी के लिए एक उत्पाद स्पष्ट है।WooCommerce - कार्ट
उदाहरण:
आप टोकरी में "product1" जोड़ते हैं, और टोकरी अब 2 उत्पादों को दिखाएगी। "उत्पाद 1" और "फ्रीबी"। जब आप टोकरी से उत्पाद को हटाते हैं, तो फ्रीबी फिर से हटा दी जाएगी।
मैं अब के लिए इस कोड को मिला:
add_action('woocommerce_add_to_cart', 'check_freebie_exists', 10, 6);
function check_freebie_exists($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
if($product_id == 1234) { // obviously replace this with the product that triggers the freebie
/* or you could use
if(has_term($cat_id, 'product_cat', $product_id)) { to check if a product is in a particular product_cat
or you could check anything else related to the product
*/
$hasfreebie = false;
// loop through the cart to check the freebie is not already there
global $woocommerce;
$cart = $woocommerce->cart->get_cart();
foreach($cart as $key => $values) {
if($values['data']->id == $your_freebie_product_id) {
$hasfreebie = true;
break;
}
}
if(!$hasfreebie) {
$woocommerce->cart->add_to_cart($your_freebie_product_id);
}
}
}
add_action('woocommerce_cart_item_removed', 'remove_freebie', 10, 2);
function remove_freebie($cart_item_key, $cart) {
$hasmaster = false;
$freebiekey = NULL;
foreach($cart as $key => $values) {
if($values['data']->id == 1234) { // check that we have the product that should trigger the freebie
$hasmaster = true;
} elseif($values['data']->id == $your_freebie_product_id) {
$freebiekey = $key;
}
}
if(!$hasmaster && $freebiekey) {
$cart->remove_cart_item($freebiekey);
}
}
लेकिन यह अभी तक काम करने लगता है।
मैं क्या गलत कर रहा हूं?
किसी भी मदद की वास्तव में सराहना की जाएगी।
धन्यवाद।
धन्यवाद, मैं इसे ASAP कर दूंगा। –