php - 仅当在 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;
}
解决方案
推荐阅读
- windows - postgres:强制本地连接使用没有 pg_hba.conf 的密码
- javascript - 为什么我不能将 borderRadius 应用到这个 React 引导选项卡?
- postgresql - Acces ODBC passthru 查询失败,错误 3146 给出不同的错误描述文本
- python - 在 WxPython 中的 StaticBitmap 上放置一个位图
- javascript - 如何在浏览器中使用带有纯 html 和 javascript 的 IPFS
- typescript - 具有泛型类型并使用字符串作为默认参数的函数
- javascript - 具有特定 URL 的 azure devops 中的 Testcafe
- python - request.body 中的 i 错误:TypeError: 'builtin_function_or_method' 对象不可迭代
- pandas - Fillna by Gruopby pandas Dataframe 从其他列中的值中选择最常见的值
- javascript - 为什么这个阴影 DOM 高度只有 100%,有两个高度?