向woocommerce购物车中添加费用
问题描述:
当客户拥有特定的结算国家/地区时,我想为我的购物车添加费用。例如比利时(比利时)向woocommerce购物车中添加费用
我发现此代码默认添加费用。 任何人都可以帮助我一个IF公式或什么,所以它只适用于开票国家= BE?
add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if (is_admin() && ! defined('DOING_AJAX'))
return;
$percentage = 0.01;
$surcharge = ($woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total) * $percentage;
$woocommerce->cart->add_fee('Surcharge', $surcharge, true, '');
}
答
这就是你需要的。只需将BE添加到$county
数组中(我只是在下面的代码中为你做了这个)。
/**
* Add a 1% surcharge to your cart/checkout based on delivery country
* Taxes, shipping costs and order subtotal are all included in the surcharge amount
*
* Change $percentage to set the surcharge to a value to suit
*
* Add countries to array('BE'); to include more countries to surcharge
* http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes for available alpha-2 country codes
*
* Change in_array to !in_array to EXCLUDE the $countries array from surcharges
*
* Uses the WooCommerce fees API
* Add to theme functions.php
*/
add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge');
function woocommerce_custom_surcharge() {
global $woocommerce;
if (is_admin() && ! defined('DOING_AJAX'))
return;
$county = array('BE');
$percentage = 0.01;
if (in_array($woocommerce->customer->get_shipping_country(), $county)) :
$surcharge = ($woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total) * $percentage;
$woocommerce->cart->add_fee('Surcharge', $surcharge, true, '');
endif;
}
来源:https://docs.woothemes.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/
+0
谢谢埃里克!我之前也发现了这个代码,但它对我没有用。不知何故,当我从你的答案中复制它时,它确实有效。 你能帮我进一步处理这段代码吗?我还有另一个问题。 – DigiStef
+0
当然,您可以在LinkedIn上向我发送您的问题 –
你应该分享您尝试应用代码。 –
你是对的:)对不起,忘了代码... – DigiStef