2015-12-02 176 views
-1

當客戶擁有特定的結算國家/地區時,我想爲我的購物車添加費用。例如比利時(比利時)向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, ''); 

} 
+0

你應該分享您嘗試應用代碼。 –

+0

你是對的:)對不起,忘了代碼... – DigiStef

回答

1

這就是你需要的。只需將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上向我發送您的問題 –

相關問題