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

Applied coupons disable Free shipping conditionally in Woocommerce

我正在努力做到这一点,这样如果客户要添加优惠券代码(其中任何一个),免费送货选项就会消失,并且会实施统一费率。 - 你会认为这将是一件容易实现的事情,会有 100 种插件和方法被描述来做到这一点,但我还没有找到任何。我不想为插件支付 89 美元来做这件事

一个额外的好处是,如果他们使用优惠券但消费超过 249 美元,他们仍然有资格享受免费送货。我读了一些如何做到这一点的地方,但它需要我获得 POST ID,这是不可能像以前那样使用最新的 WooCommerce,我不知道运输 ID,所以我迷路了这里是代码

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;
} );

谢谢

已编辑

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;

}

默认情况下,如果优惠券代码已填写并在购物车中处于活动状态,WooCommerce 仍将允许选择免费送货方式。如果使用优惠券代码,可以将以下代码添加到您的 child 的 functions.php 页面以挂钩并隐藏任何免费送货选项。

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;
} );

只需将上面代码中的 $free_shipping_id 更改为您的免费送货选项的 ID。您可以转到 WooCommerce > 设置 > 送货选项找到您的 ID,然后单击您的免费送货选项。 post ID 将在显示免费送货的页面的 URL 中 details/settings。

i.e www.yourdomain.com/wp-admin/post.php?post=40107&action=edit

在此示例中,40107 是运输 ID。您的送货 ID 会有所不同。

仅当购物车小计达到最低金额时,以下代码才会为应用的优惠券启用“免费送货”(折扣含税)

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;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。


原回答:

下面的代码将在无需任何设置的情况下应用任何优惠券时删除“免费送货”送货方式。您的实际代码中存在一些错误。尝试以下操作:

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;
}

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