在 WordPress 中使用 Woocommerce
我正在尝试找到一种使用 2 个运费的方法:
如果客户订单少于 n 金额,则运费为标准 x 金额
如果客户订单超过 n 金额,则运费将减少至 x 金额
如果订单符合减少运费的条件,我还需要隐藏标准。
目前,如果订单符合减少运费的条件,woocommerce 会显示两种运费
有什么想法可以解决这个问题吗?
谢谢
最佳答案
这是一个 Hook 在 woocommerce_package_rates
操作 Hook 中的自定义函数,该函数将根据购物车金额限制仅显示一种运费:
add_filter( 'woocommerce_package_rates', 'hide_sshipping_rate_based_on_cart_amount', 100 );
function hide_sshipping_rate_based_on_cart_amount( $rates ) {
// HERE define the limit amount to switch of shipping rate (integer)
$amout_limit = 600;
// Cart total amount (integer)
$cart_total = WC()->cart->cart_contents_total;
// Set below your 2 Rate ID slugs
if( $cart_total >= $amout_limit )
unset( $rates['flat_rate:12'] ); // Removes the LOWER Rate
else
unset( $rates['flat_rate:8'] ); // Removes the HIGHER Rate
return $rates;
}
代码位于事件子主题(或主题)的 function.php 文件中或任何插件文件中。
关于php - 根据购物车数量仅显示一种运费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44969477/