2017-09-20 49 views
1

在WooCommerce,我使用下面的代碼添加$ 12個收費在functions.php文件中的子主題的銷量爲加拿大客戶爲我的客戶之一。定製woocommerce購物車收費取決於產品種類(非PDF)

但我需要刪除的收費全部下載PDF文件。

這可能改變我使用的代碼?

這裏是我的代碼:

add_action('woocommerce_cart_calculate_fees','xa_add_surcharge'); 
function xa_add_surcharge() { 
    global $woocommerce; 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    $county  = array('CA'); 
    $fee = 12.00; 

    if (in_array($woocommerce->customer->get_shipping_country(), $county)) : 
     $surcharge = + $fee; 
     $woocommerce->cart->add_fee('Surcharge for International Orders', $surcharge, true, ''); 
    endif; 
} 

回答

0

它在非下載的產品(或者根據您的離子PDF產品爲非虛擬產品的設置)車項目可能的檢查。

此外,我已經重新審視你的代碼位:

add_action('woocommerce_cart_calculate_fees','add_custom_surcharge', 10, 1 ); 
function add_custom_surcharge($wc_cart) { 
    if (is_admin() && ! defined('DOING_AJAX')) return; 

    $countries = array('CA'); // Defined countries 

    // Continue only for defined countries 
    if(! in_array(WC()->customer->get_shipping_country(), $countries)) return; 

    $fee_cost = 12; // The Defined fee cost 
    $downloadable_only = true; 

    // Checking cart items for NON downloadable products 
    foreach ($wc_cart->get_cart() as $cart_item_key => $cart_item) { 
     // Checks if a product is not downloadable. 
     if(! $cart_item['data']->is_downloadable()){ // or cart_item['data']->is_virtual() 
      $downloadable_only = false; 
      break; 
     } 
    } 
    // If one product is not downloadable and if customer shipping country is Canada we add the fee 
    if (! $downloadable_only) 
     $wc_cart->add_fee("Surcharge for International Orders", number_format($fee_cost, 2), true); 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

所有代碼都在Woocommerce 3+和工程測試。

+0

謝謝soooooo了,這完美地工作,託尼 –

相關問題