首页 > 解决方案 > 仅当在 WooCommerce 中使用特定优惠券时才适用运费

问题描述

我需要创建 2000 张优惠券来销售,但我希望使用它们的客户始终支付运费。目前获得免费送货的门槛设置为 69 欧元以上。我尝试使用下面的代码(取自此处:应用优惠券在 Woocommerce 中有条件地禁用免费送货)。

它适用于所有优惠券,我只想将其应用于带有前缀“pd”的优惠券。

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

标签: phpwordpresswoocommercecoupon

解决方案


推荐阅读