doujiong3146 2013-12-26 18:02
浏览 24
已采纳

如何使用美元金额变量在WooCommerce中应用自动折扣

In my attempt to find a solution for dollar amount based automatic coupon I am encountering the following problem. First the rules:

  1. If dollar amount less than 53 do nothing.
  2. If dollar amount greater than 53 and less than 90 apply "couponX"
  3. If dollar amount greater than 90 apply "couponY"

I have been able to get the code to work with the second (2) rule of the code. However the third rule will not be applied. My code below. Any thoughts?

// Add coupon when user views cart before checkout
add_action('woocommerce_before_cart_table', 'add_coupon_automatically');
add_action('woocommerce_before_checkout_form', 'add_coupon_automatically');

// Check if function? create it.
if (!function_exists('add_coupon_automatically')) {
function add_coupon_automatically() {
global $woocommerce;
$eleven = '20plus';
$four = '10plus';
$eleven_minimum = 179;
$four_minimum = 89;

if($woocommerce->cart->get_cart()->cart_contents_total>$eleven_minimum) {
// Apply 20plus
if (!$woocommerce->cart->add_discount( sanitize_text_field( $eleven )))
$woocommerce->add_message('20 Plus coupon (11%) automatically applied!');
$woocommerce->show_messages();
//Recalculate totals
 $woocommerce->cart->calculate_totals();
} elseif ($woocommerce->cart->get_cart()->cart_contents_total<$eleven_minimum 
  && $woocommerce->cart->get_cart()->cart_contents_total>$four_minimum) {
// Apply 10plus
if (!$woocommerce->cart->add_discount( sanitize_text_field( $four )))
$woocommerce->add_message('10 Plus coupon (4%) automatically applied!');
$woocommerce->show_messages();
//Recalculate totals
$woocommerce->cart->calculate_totals();
}
} 
}
  • 写回答

2条回答 默认 最新

  • dtpt75860 2014-01-02 21:42
    关注

    Was going about it the wrong way. Seems the best approach was to base it on cart Item quantity rather than price.

    New rules:

    1. Create coupons with minimum of 10 / 20 quantities.
    2. Add to child_theme/woocommerce/cart.php

      global $count_cart_quantity;
      $count_cart_quantity = $woocommerce->cart->cart_contents_count;
      
    3. Finally apply the coupons via hook

      add_action('woocommerce_before_cart_table', 'discount_20');
      function discount_20() {
      global $woocommerce;
      global $count_cart_quantity;
      if ( $count_cart_quantity > 19 ) {
      $coupon_code = '20plus';
      if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) {
          $woocommerce->show_messages();
      }
      echo '<div classs"woocommerce_message"><strong>
      You qualified for an 11% discount and it has been applied!</strong>
      </div>';
      }
      }
      
      add_action('woocommerce_before_cart_table', 'discount_10');
      function discount_10() {
      global $woocommerce;
      global $count_cart_quantity;
      if ( $count_cart_quantity < 20 && $count_cart_quantity > 9 ) {
      $coupon_code = '10plus';
      if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) {
          $woocommerce->show_messages();
      }
      echo '<div classs"woocommerce_message"><strong>
      You qualified for an 4% discount and it has been applied!</strong>
      </div>';
      }
      }
      

    Now it works well!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容