在 Woocommerce 中应用特定优惠券时隐藏免费送货

Hide free shipping when specific coupons are applied in Woocommerce

我正在尝试将其设置为特定优惠券不提供免费送货服务。

然后我尝试了这个,但它不起作用

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
    $applied_coupons = WC()->session->get( 'applied_coupons', array() );
    if ( ! empty( $applied_coupons ) ) {
        
        if (in_array("544", $applied_coupons))
        {
        
            $free_shipping_id_11 = 'free_shipping:11';
            if($free_shipping_id_11){
                unset($packages[0]['rates'][ $free_shipping_id_11 ]);
            }
            $free_shipping_id_9 = 'free_shipping:9';
            if($free_shipping_id_9){
                unset($packages[0]['rates'][ $free_shipping_id_9 ]);
            }
        
        }
        
    }
    return $packages;
} );

请尝试以下操作:

add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
function filter_woocommerce_package_rates( $rates, $package ) {
    $targeted_coupons = array("544"); // Here set your related coupon codes

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

    if ( ! empty($applied_coupons) && array_intersect( $targeted_coupons, $applied_coupons ) ) {
        foreach ( $rates as $rate_key => $rate ) {
            if ( 'free_shipping' === $rate->method_id ) {
                unset($rates[$rate_key]);
            }
        }
    }
    return $rates;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

Refresh the shipping caches:

  1. This code is already saved on your functions.php file.
  2. In a shipping zone settings, disable / save any shipping method, then enable back / save.

    You are done and you can test it.