运费仅在WooCommerce中使用了特定优惠券时适用

时间:2019-03-08 11:17:06

标签: php wordpress woocommerce coupon

我需要创建2000张优惠券才能销售,但是我希望使用它们的客户始终支付运费。目前,免费送货的门槛设置为69欧元以上。我尝试使用下面的代码(从此处获取:Applied coupons disable Free shipping conditionally in 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;
}

0 个答案:

没有答案