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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘