WooCommerce基于动态最低订单金额的费用
问题描述:
我需要在购物车中设置最低订单费用,因此如果购物车中的产品总价格不超过10英镑,则需要支付额外费用才能将价格提高到10英镑。WooCommerce基于动态最低订单金额的费用
这里是我目前在购物车阶段运作良好的代码,但是当您到达结账处时,定价部分因某种原因不会停止加载,您无法结帐,任何人都可以帮忙吗?从functions.php的
代码:
你正面临function woocommerce_custom_surcharge() {
global $woocommerce;
if (is_admin() && ! defined('DOING_AJAX'))
return;
$minimumprice = 10;
$currentprice = $woocommerce->cart->cart_contents_total;
$additionalfee = $minimumprice - $currentprice;
if ($additionalfee >= 0) {
wc_print_notice(
sprintf('We have a minimum %s per order. As your current order is only %s, an additional fee will be applied at checkout.' ,
wc_price($minimumprice),
wc_price($currentprice)
), 'error'
);
$woocommerce->cart->add_fee('Minimum Order Adjustment', $additionalfee, true, '');
}
}
add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge');
答
无限加载旋转的问题是由于wc_print_notice()
,当它在woocommerce_cart_calculate_fees
胡克的使用。这看起来像一个错误。
如果使用wc_add_notice()
,问题不存在但通知显示2次。
此外,我已经重新审视你的code.The 唯一的解决办法是它在2个独立的功能划分:
// NOTICE ONLY IN CART PAGE
add_action('woocommerce_cart_calculate_fees', 'add_custom_surcharge', 10, 1);
function add_custom_surcharge($cart_object) {
if (is_admin() && ! defined('DOING_AJAX'))
return;
$minimumprice = 100;
$currentprice = $cart_object->cart_contents_total;
$additionalfee = $minimumprice - $currentprice;
if ($additionalfee >= 0) {
$cart_object->add_fee('Minimum Order Adjustment', $additionalfee, true);
if(! is_checkout()){
$message = sprintf(__('We have a minimum %s per order. As your current order is only %s, an additional fee will be applied.', 'woocommerce'), wc_price($minimumprice), wc_price($currentprice));
wc_print_notice($message, 'error');
}
}
}
// NOTICE ONLY IN CHECKOUT PAGE
add_action('woocommerce_before_checkout_form', 'custom_surcharge_message', 10, 0);
function custom_surcharge_message() {
$cart_object = WC()->cart;
$minimumprice = 100;
$currentprice = $cart_object->cart_contents_total;
$additionalfee = $minimumprice - $currentprice;
if ($additionalfee >= 0) {
$message = sprintf(
__('We have a minimum %s per order. As your current order is only %s, an additional fee will be applied.', 'woocommerce'),
wc_price($minimumprice), wc_price($currentprice)
);
wc_print_notice($message, 'error');
}
}
代码放在您的活动子主题(或主题)的function.php文件或者也在任何插件文件中。
经过测试,完美适用于WooCommerce 3+