doupeng6890 2018-10-21 04:44
浏览 127
已采纳

应用优惠券禁用Woocommerce中的有条件免费送货

I am trying to get it so that if a customer were to add a coupon code (Any of them) the Free Shipping option would go away and the flat rate fee would be implemented. - You would think this would be an easy thing to implement, there would be 100's of plugins and ways described to do this, but I have not found any. I do not want to pay $89 for the plugin to do this one thing

A side bonus would be if they are using a coupon but are spending over $249 they can still qualify for Free shipping. I read some where how to do this, but it requires me to get the POST ID, which with the latest WooCommerce is not possible like it was before, I do not know the shipping ID so I am at a lost Here is the code

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
$applied_coupons = WC()->session->get( 'applied_coupons', array() );
if ( ! empty( $applied_coupons ) ) {
$free_shipping_id = 'free_shipping:2';
unset($packages[0]['rates'][ $free_shipping_id ]);
}
return $packages;
} );

Thanks

Edited

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 
10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return $rates;

$min_total      = 250; // Minimal subtotal allowing free shipping

// Get needed cart totals
$total_excl_tax = WC()->cart->get_total();
$discount_excl_tax = WC()->cart->get_discount_total();


// Calculating the discounted subtotal including taxes
$discounted_subtotal_incl_taxes = $total_excl_tax - $discount_excl_tax;

$applied_coupons   = WC()->cart->get_applied_coupons();

if( sizeof($applied_coupons) > 0 &&  $discounted_subtotal_incl_taxes < $min_total ) {
    foreach ( $rates as $rate_key => $rate ){
        // Targeting "Free shipping"
        if( 'free_shipping' === $rate->method_id  ){
            unset($rates[$rate_key]);
        }
    }
}
return $rates;

}

  • 写回答

2条回答 默认 最新

  • douyong1850 2018-10-21 07:25
    关注

    The below code will enable "Free shipping" for applied coupons only if cart subtotal reaches a minimal amount (discounted including taxes):

    add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
    function coupons_removes_free_shipping( $rates, $package ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return $rates;
    
        $min_subtotal      = 250; // Minimal subtotal allowing free shipping
    
        // Get needed cart subtotals
        $subtotal_excl_tax = WC()->cart->get_subtotal();
        $subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
        $discount_excl_tax = WC()->cart->get_discount_total();
        $discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();
    
        // Calculating the discounted subtotal including taxes
        $discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;
    
        $applied_coupons   = WC()->cart->get_applied_coupons();
    
        if( sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
            foreach ( $rates as $rate_key => $rate ){
                // Targeting "Free shipping"
                if( 'free_shipping' === $rate->method_id  ){
                    unset($rates[$rate_key]);
                }
            }
        }
        return $rates;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and work.


    Original answer:

    The bellow code will remove "Free shipping" shipping methods when any coupon is applied without any settings need. There is some mistakes in your actual code. Try the following:

    add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
    function coupons_removes_free_shipping( $rates, $package ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return $rates;
    
        $applied_coupons = WC()->cart->get_applied_coupons();
    
        if( sizeof($applied_coupons) > 0 ){
            // Loop through shipping rates
            foreach ( $rates as $rate_key => $rate ){
                // Targeting "Free shipping" only
                if( 'free_shipping' === $rate->method_id  ){
                    unset($rates[$rate_key]); // Removing current method
                }
            }
        }
        return $rates;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

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

报告相同问题?

悬赏问题

  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码