php - 在 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;
}
解决方案
仅当购物车小计达到最低金额(折扣含税)时,以下代码才会为应用的优惠券启用“免费送货” :
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 文件中。测试和工作。
推荐阅读
- request - 检查 Laravel 自定义验证规则中是否存在路由参数?
- javascript - 在 React JS 中使用 Fetch 获取 API 数据
- date - 在 Go 中解析带有负年份的日期?
- java - 为什么当我导入我的包 X.* - java 会抛出一个错误但如果我直接指定类(例如:X.Linked_List)没有错误
- java - 获取其他应用的通知渠道(或通知设置)
- ios - IOS/Xcode/Git:突然无法提交更改,无法自动检测电子邮件地址错误
- api - 麻烦下载npm包
- c - LMDB 作案手法
- java - 用于在字符串中查找 1 到 3 个字符的正则表达式
- pycharm - pycharm 中为每个属性(字段)生成自动 setter 和 getter 函数并生成自动承包商的功能