【问题标题】:Auto apply a percentage or fixed cart discount based on the total in WooCommerce根据 WooCommerce 中的总数自动应用百分比或固定购物车折扣
【发布时间】:2018-06-24 05:07:01
【问题描述】:

我正在尝试在我客户的 WooCommerce 网站上设置优惠券,以便在购物车总金额低于上限或固定金额等于或大于上限金额时应用百分比折扣。

假设购物车总数的上限为 200。如果购物车总数低于此上限,则应用 10% 的折扣。但如果购物车总数为 200 或更多,则应用固定金额 20 作为折扣。

例如:

  • 我的购物车总数是 190。由于这小于 200 的上限,因此折扣金额计算为 10%,即应用 19
  • 我的购物车总数是 210。由于这大于 200 的上限,因此应用了 20 的固定数量。

如何将我的 WooCommerce 设置为根据总数应用百分比折扣或固定购物车?

【问题讨论】:

    标签: php wordpress woocommerce cart coupon


    【解决方案1】:

    您可以使用挂在 woocommerce_before_calculate_totals 动作挂钩中的自定义函数,您将在其中定义 2 个优惠券代码:

    • 百分比折扣优惠券代码(10%)
    • 固定金额的折扣券代码(20 美元)

    代码:

    add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupons_total_based', 10, 1 );
    function auto_add_coupons_total_based( $cart ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // HERE define your coupon code
        $coupon_percent = 'uget10percent'; # <===  <===  <===  <===  <===  <===
        $coupon_fixed = 'uget20off'; # <===  <===  <===  <===  <===  <===  <===
    
        // Get cart subtotal
        $subtotal = 0;
        foreach($cart->get_cart() as $cart_item ){
            $subtotal += $cart_item['line_subtotal'];
            $subtotal += $cart_item['line_subtotal_tax']; // with taxes
        }
    
        // Coupon type "percent" (less than 200)
        if( $subtotal < 200 && ! $cart->has_discount( $coupon_percent ) ){
            // If coupon "fixed amount" type is in cart we remove it
            if( $cart->has_discount( $coupon_fixed ) )
                $cart->remove_coupon( $coupon_fixed );
    
            // Apply the "percent" type coupon code
            $cart->add_discount( $coupon_percent );
        }
        // Coupon type "fixed amount" (Up to 200)
        elseif( $subtotal >= 200 && ! $cart->has_discount( $coupon_fixed ) ) {
            // If coupon "percent" type is in cart we remove it
            if( $cart->has_discount( $coupon_percent ) )
                $cart->remove_coupon( $coupon_percent );
    
            // Apply the "fixed amount" type coupon code
            $cart->add_discount( $coupon_fixed );
        }
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且有效。

    如果您想将其应用于小计不含税,您必须评论此行:

    $subtotal += $cart_item['line_subtotal_tax']; // with taxes
    

    或者您也可以通过这种方式使用负费用(即折扣)代替优惠券:

    add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total', 25, 1 );
    function discount_based_on_total( $cart ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
        $total = $cart->cart_contents_total;
    
        // Percentage discount (10%)
        if( $total < 200 )
            $discount = $total * 0.1;
        // Fixed amount discount ($20)
        else
            $discount = 20;
    
        // Add the discount
        $cart->add_fee( __('discount', 'woocommerce'), -$discount );
    }
    

    代码进入活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且有效。

    【讨论】:

    • 这看起来很有希望 - 我知道代码是如何工作的,我会试一试。我忘了提到折扣规则仅​​适用于特定类别的产品。看来我会对代码进行一些调整。谢谢:-)
    • @JunDolor 一旦你尝试过这个,让我知道哪种解决方案最适合你。对于优惠券,您可以针对特定的产品类别或产品 ID 限制每个优惠券本身……这可以在第二个功能中完成,但这应该是一个新问题。
    • 感谢@LoicTheAztec 我已经尝试了第一个选项并且它有效。但是,优惠券会立即应用。我希望用户在代码运行之前输入优惠券代码并申请。我尝试使用钩子 woocommerce_applied_coupon,但无法运行代码。我仍在尝试解决问题。我会提供更新
    • 在此@LoicTheAztec 上注明,您的答案现在已被接受,(并赞赏:-))。我会准备后续问题。谢谢
    • @JunDolor 这正是我想要的。您是否调整了仅适用于特定类别产品的折扣规则?如果是这样如何实现这一目标?我尝试通过包含产品 ID 来应用累进折扣,但这不起作用。
    【解决方案2】:

    好的,所以我终于弄清楚了如何使用此代码,以便它仅在添加优惠券时触发。我还添加了一个脚本来通知买家已达到优惠券折扣上限

    add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupons_total_based', 10, 1 ); function auto_add_coupons_total_based( $cart ) {
    
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    // HERE define your coupon code
    $coupon_percent = 'xyz20'; # <===  <===  <===  <===  <===  <===
    $coupon_fixed = 'fixedamount'; # <===  <===  <===  <===  <===  <===  <===
    
    // Get cart subtotal
    $subtotal = 0;
    foreach($cart->get_cart() as $cart_item ){
        $subtotal += $cart_item['line_subtotal'];
        $subtotal += $cart_item['line_subtotal_tax']; // with taxes
    }
    
    //Set HERE the limit amount
    $limit = 40; //without Tax
    
    
    // Coupon type "percent" (less than 200)
    if( $subtotal < 200 && ! $cart->has_discount( $coupon_percent ) ){
        // If coupon "fixed amount" type is in cart we remove it
        if( $cart->has_discount( $coupon_fixed ) )
            $cart->remove_coupon( $coupon_fixed );
    
    
    }
    // Coupon type "fixed amount" (Up to 200)
    if( $subtotal >= 200 && $cart->has_discount( $coupon_percent ) ) {
        // If coupon "percent" type is in cart we remove it
        if( $cart->has_discount( $coupon_percent ) )
            $cart->remove_coupon( $coupon_percent );
    
        // Apply the "fixed amount" type coupon code
        $cart->add_discount( $coupon_fixed );
    
    
        // Displaying a custom message
            $message = __( "The total discount limit of $$limit has been reached", "woocommerce" );
            wc_add_notice( $message, 'notice' );
    
    
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 2020-11-27
      • 1970-01-01
      • 2017-10-08
      • 2020-11-21
      • 1970-01-01
      相关资源
      最近更新 更多