2013-04-01 10 views
27

Woocommerce में मैं कार्ट में वजन 100 एलबीएस से अधिक होने पर पूरे ग्राहक के आदेश पर 10% छूट लागू करने का एक तरीका खोजने का प्रयास कर रहा हूं। मैं इसे प्राप्त करने का एक हिस्सा हूं। अगले चरण के लिए, मैं functions.php के माध्यम से क्रिया/हुक के माध्यम से एक कूपन कोड प्रोग्रामेटिक रूप से लागू करने का एक तरीका ढूंढ रहा हूं।मैं प्रोग्रामिंग रूप से Woocommerce में कूपन कैसे लागू करूं?

ऐसा प्रतीत होता है कि मैं यह करने के लिए फ़ंक्शन woocommerce_ajax_apply_coupon का उपयोग कर सकता हूं (http://docs.woothemes.com/wc-apidocs/function-woocommerce_ajax_apply_coupon.html) लेकिन मुझे इसका उपयोग करने के बारे में अनिश्चितता है।

अब तक मैंने कार्ट में सभी उत्पादों का कुल वजन प्राप्त करने के लिए cart.php को संशोधित किया है, मैंने एक कूपन बनाया है जो छूट लागू करता है (यदि मैन्युअल रूप से दर्ज किया गया है) और मैंने कार्यों में कुछ कोड जोड़ा है वजन जांचने और उपयोगकर्ता को एक संदेश प्रदर्शित करने के लिए .php।

संपादित करें: आंशिक कोड हटा दिया गया है, नीचे दिए गए समाधान में पूरा कोड शामिल है।


मार्गदर्शन फ्रेनी के लिए धन्यवाद। यहाँ काम कर अंतिम परिणाम जो सफलतापूर्वक डिस्काउंट कूपन जब स्थिति उत्पन्न होने पर लागू होता है है और यह भी जब यह नहीं रह गया है से मुलाकात की है उसे निकाल देता है:

/* Mod: 10% Discount for weight greater than 100 lbs 
Works with code added to child theme: woocommerce/cart/cart.php lines 13 - 14: which gets $total_weight of cart: 
     global $total_weight; 
     $total_weight = $woocommerce->cart->cart_contents_weight; 
*/ 
add_action('woocommerce_before_cart_table', 'discount_when_weight_greater_than_100'); 
function discount_when_weight_greater_than_100() { 
    global $woocommerce; 
    global $total_weight; 
    if($total_weight > 100) { 
     $coupon_code = '999'; 
     if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) { 
      $woocommerce->show_messages(); 
     } 
     echo '<div class="woocommerce_message"><strong>Your order is over 100 lbs so a 10% Discount has been Applied!</strong> Your total order weight is <strong>' . $total_weight . '</strong> lbs.</div>'; 
    } 
} 

/* Mod: Remove 10% Discount for weight less than or equal to 100 lbs */ 
add_action('woocommerce_before_cart_table', 'remove_coupon_if_weight_100_or_less'); 
function remove_coupon_if_weight_100_or_less() { 
    global $woocommerce; 
    global $total_weight; 
    if($total_weight <= 100) { 
     $coupon_code = '999'; 
     $woocommerce->cart->get_applied_coupons(); 
     if (!$woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) { 
      $woocommerce->show_messages(); 
     } 
     $woocommerce->cart->calculate_totals(); 
    } 
} 

उत्तर

27

सबसे पहले, एक डिस्काउंट कूपन (http://docs.woothemes.com/document/create-a-coupon-programatically/ के माध्यम से) बनाने के लिए:

$coupon_code = 'UNIQUECODE'; // Code - perhaps generate this from the user ID + the order ID 
$amount = '10'; // Amount 
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product 

$coupon = array(
    'post_title' => $coupon_code, 
    'post_content' => '', 
    'post_status' => 'publish', 
    'post_author' => 1, 
    'post_type'  => 'shop_coupon' 
);  

$new_coupon_id = wp_insert_post($coupon); 

// Add meta 
update_post_meta($new_coupon_id, 'discount_type', $discount_type); 
update_post_meta($new_coupon_id, 'coupon_amount', $amount); 
update_post_meta($new_coupon_id, 'individual_use', 'no'); 
update_post_meta($new_coupon_id, 'product_ids', ''); 
update_post_meta($new_coupon_id, 'exclude_product_ids', ''); 
update_post_meta($new_coupon_id, 'usage_limit', '1'); 
update_post_meta($new_coupon_id, 'expiry_date', ''); 
update_post_meta($new_coupon_id, 'apply_before_tax', 'yes'); 
update_post_meta($new_coupon_id, 'free_shipping', 'no'); 
पिछले फ़ंक्शन

if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) 
    $woocommerce->show_messages(); 

यह एक बूल मान:

उसके बाद अपना आदेश है कि कूपन लागू सही अगर छूट, सफल गलत था अगर यह किसी पर के लिए विफल रहता है विभिन्न कारणों से ई।

+0

चूंकि मेरे पास बैकएंड में पहले से ही एक कूपन है: "999" मैंने केवल कूपन लागू करने के लिए प्रदान किया गया कोड जोड़ा है। हालांकि, जब मैं woocommerce कार्ट पेज पर जाता हूं तो मुझे अब त्रुटि मिल रही है: लाइन 65 पर [पथ से ...] functions.php में गैर-ऑब्जेक्ट पर किसी सदस्य फ़ंक्शन add_discount() पर कॉल करें [add_discount के साथ if statement]। मैंने यह दिखाने के लिए कि यह कैसा दिखता है, मैंने अपनी मूल पोस्ट में कोड अपडेट किया है। – msargenttrue

+1

अपनी '$ कुल_वेट' लाइन के साथ 'वैश्विक $ woocommerce;' जोड़ने का प्रयास करें। – Freney

+0

धन्यवाद, यह काम किया! तो अब मैं छूट सफलतापूर्वक लागू करने में सक्षम हूं लेकिन अब मैं एक और मुद्दा देख रहा हूं ... यदि उपयोगकर्ता कुछ कार्ट आइटम हटा देता है और गाड़ी वापस 100 एलबीएस या उससे कम हो जाती है तो छूट अभी भी वहां है। यदि डिस्काउंट अब शर्त को पूरा नहीं करता है तो मैं छूट को हटाने का एक तरीका ढूंढ रहा हूं ताकि उपयोगकर्ता इस कूपन का फायदा उठाने में सक्षम न हों। मैं उम्मीद करता हूं कि वहाँ एक remove_discount फ़ंक्शन होगा जो add_discount के विपरीत तरीके से काम करता है लेकिन मैं Woocommerce API दस्तावेज़ों में से कोई एक नहीं ढूंढ पाया: http://docs.woothemes.com/wc-apidocs/ – msargenttrue

6

मैंने इस समाधान का उपयोग किया, लेकिन इसमें एक बग शामिल है क्योंकि ओपी ने इसे लिखा था। यदि उपयोगकर्ता कार्ट का पूर्वावलोकन छोड़ देता है और सीधे चेकआउट फॉर्म पर जाता है, तो यह कूपन लागू नहीं करता है। मेरा समाधान यहाँ था।

// Independence day 2013 coupon auto add 
// Add coupon when user views cart before checkout (shipping calculation page). 
add_action('woocommerce_before_cart_table', 'add_independence_day_2013_coupon_automatically'); 

// Add coupon when user views checkout page (would not be added otherwise, unless user views cart first). 
add_action('woocommerce_before_checkout_form', 'add_independence_day_2013_coupon_automatically'); 

// Check if php function exists. If it doesn't, create it. 
if (!function_exists('add_independence_day_2013_coupon_automatically')) { 

    function add_independence_day_2013_coupon_automatically() { 

     global $woocommerce; 
     $coupon_code = 'independencedaysale'; 
     $bc_coupon_start_date = '2013-06-30 17:00:00'; 
     $bc_coupon_end_date = '2013-07-08 06:59:59'; 

     // Only apply coupon between 12:00am on 7/1/2013 and 11:59pm on 7/7/2013 PST. 
     if ((time() >= strtotime($bc_coupon_start_date)) && 
      (time() <= strtotime($bc_coupon_end_date))) { 

      // If coupon has been already been added remove it. 
      if ($woocommerce->cart->has_discount(sanitize_text_field($coupon_code))) { 

       if (!$woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) { 

        $woocommerce->show_messages(); 

       } 

      } 

      // Add coupon 
      if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) { 

       $woocommerce->show_messages(); 

      } else { 

       $woocommerce->clear_messages(); 
       $woocommerce->add_message('Independence day sale coupon (10%) automatically applied'); 
       $woocommerce->show_messages(); 

      } 

      // Manually recalculate totals. If you do not do this, a refresh is required before user will see updated totals when discount is removed. 
      $woocommerce->cart->calculate_totals(); 

     } else { 

      // Coupon is no longer valid, based on date. Remove it. 
      if ($woocommerce->cart->has_discount(sanitize_text_field($coupon_code))) { 

       if ($woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) { 

        $woocommerce->show_messages(); 

       } 

       // Manually recalculate totals. If you do not do this, a refresh is required before user will see updated totals when discount is removed. 
       $woocommerce->cart->calculate_totals(); 

      } 

     } 

    } 

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