2013-04-01 102 views
27

在Woocommerce中,如果購物車的重量超過100磅,我試圖找到一種方法對整個客戶的訂單應用10%的折扣。我是實現這一目標的一部分。對於下一步,我正在尋找一種方法來通過functions.php通過action/hook以編程方式應用優惠券代碼。如何以編程方式在Woocommerce中應用優惠券?

看來我可以使用函數woocommerce_ajax_apply_coupon來做到這一點(http://docs.woothemes.com/wc-apidocs/function-woocommerce_ajax_apply_coupon.html),但我不確定如何使用它。

到目前爲止,我已經修改cart.php得到的所有產品的總重量的車,我已經創建了一個應用折扣(如果手動輸入)的優惠券,我已經添加了一些代碼的功能.php來檢查重量並向用戶顯示一條消息。

編輯:刪除了部分代碼,完成的代碼包含在下面的解決方案中。


感謝您的指導Freney。下面是它成功地適用於當條件滿足的折扣優惠券的工作最終的結果,也刪除它時,它不再滿足:

/* 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(); 

這最後函數返回一個布爾值:如果折扣成功,FALSE如果失敗,任何對由於各種原因。

+0

由於我已經在後端有一個優惠券:「999」我只添加了您提供的代碼來應用優惠券。但是,當我轉到woocommerce購物車頁面時,我現在收到錯誤:在第65行[if語句與add_discount]的[path to ...] functions.php中的非對象上調用成員函數add_discount()。我更新了原始帖子中的代碼,向您展示了它的外觀。 – msargenttrue

+1

嘗試添加'global $ woocommerce;'以及'$ total_weight'行。 – Freney

+0

謝謝,工作!所以,現在我能夠成功應用折扣,但現在我注意到另一個問題...如果用戶刪除了一些購物車物品,並且購物車回到100磅或更少,那麼折扣仍然存在。如果購物車不再符合條件,用戶將無法利用此優惠券,我正在尋找一種方法來取消折扣。我期望有一個remove_discount函數,它的作用方式與add_discount相反,但我無法在Woocommerce API文檔中找到它:http://docs.woothemes.com/wc-apidocs/ – msargenttrue

6

我使用了這個解決方案,但它包含了一個錯誤,因爲OP寫了它。如果用戶跳過預覽購物車並直接前往結帳表單,則不會應用優惠券。這是我的解決方案。

// 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(); 

      } 

     } 

    } 

} 
相關問題