定制woocommerce购物车收费取决于产品种类(非PDF)

问题描述:

在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; 
} 

它在非下载的产品(或者根据您的离子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了,这完美地工作,托尼 –