2016-08-30 17 views
6

से एक फ्रीबी उत्पाद ऑटो को ऑटो या ऑटो हटाएं, मैं एक 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); 
    } 
} 

लेकिन यह अभी तक काम करने लगता है।

मैं क्या गलत कर रहा हूं?

किसी भी मदद की वास्तव में सराहना की जाएगी।

धन्यवाद।

+0

धन्यवाद, मैं इसे ASAP कर दूंगा। –

उत्तर

2

- अपडेट किया गया -(परीक्षण किया है और सभी उत्पादों के लिए काम कर रहे)

मैं अपने कोड को अद्यतन किया है, यह काम करने के लिए। इस कोड का परीक्षण किया जाता है और पूरी तरह से कार्य:

add_action('woocommerce_add_to_cart', 'check_add_freebie', 10, 6); 

function check_add_freebie($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) { 
    $item_special = $product_id; 
    $freebie_item_id = 99; // the freebie product 

    // just for our special product 
    if($product_id == $item_special) { 
     $cart_items = WC()->cart->get_cart(); 
     // Iterating through each item in cart 
     foreach ($cart_items as $cart_item){ 
      // If freebie product is in cart 
      if($cart_item['data']->id == $freebie_item_id) { 
       $has_freebie = true; 
       break; 
      } 
      // If freebie product is NOT in cart 
      else 
       $has_freebie = false; 
     } 
     // If freebie product is NOT in cart, we add it. 
     if(!$has_freebie) 
      WC()->cart->add_to_cart($freebie_item_id); 
    } 
} 

add_action('woocommerce_cart_item_removed', 'remove_freebie', 10, 2); 

function remove_freebie($cart_item_key, $product_id) { 
    $item_special = $product_id; 
    $freebie_item_id = 99; // the freebie product 
    $hasmaster = false; 
    $freebiekey = NULL; 
    // Iterating through each item in cart 
    foreach(WC()->cart->get_cart() as $item_key => $item) { 
     if ($item['data']->id == $item_special) 
      $hasmaster = true; // Special product is in cart 
     elseif ($item['data']->id == $freebie_item_id) 
      $freebiekey = $item_key; // Freebie product is in cart 
    } 
    // AS freebie product is in cart, it's removed too 
    if(!$hasmaster && !empty($freebiekey)) 
     WC()->cart->remove_cart_item($freebiekey); 
} 

कोड किसी भी प्लगइन फ़ाइल में अपने सक्रिय बच्चा विषय (या विषय) की function.php फ़ाइल या भी चला जाता है।

यह कोड परीक्षण और काम करता है।

+0

बहुत बहुत धन्यवाद, यह सिर्फ सही काम कर रहा है! - लेकिन एक छोटी सी समस्या है। हो सकता है कि मैंने खुद को इरादे से समझाया नहीं, लेकिन मैं अपने सभी उत्पादों के साथ टोकरी में फ्रीबी को जोड़ना चाहता हूं। तो अगर यह उत्पाद 1, 6, 27, 1042, या 621 है तो भी मैं फ्रीबी को जोड़ना चाहूंगा :-) लेकिन यदि यह केवल एक विशिष्ट उत्पाद होने का इरादा था, तो यह कोड केवल perfekt काम करता है! मुझे केवल एक श्रेणी मिली है, तो हो सकता है कि मैं item_speciel को category_speciel के साथ बदल सकता हूं? –

+0

ग्रेट साथी, मैं अपडेट के लिए इंतजार करूंगा, और अगर यह इरादे से काम करता है तो ऊपर की ओर बढ़ जाएगा। –

+0

आपको बहुत धन्यवाद, मैं यह नहीं बता सकता कि मैं कितना खुश हूं, आपने इसे काम किया है :-) सुविधा अब मेरी साइट पर पहले से ही लाइव है, और पूरी तरह से काम कर रही है! स्वीकृत और अपवोट अब आपका है :-) मुझे आशा है कि कई अन्य सहायता इसी उद्देश्य के लिए इसका उपयोग कर सकती हैं। –

संबंधित मुद्दे