2017-09-20 6 views
1

WooCommerce에서 아래 코드를 사용하여 캐나다 고객의 매출에 1232 추가 할증료를 내 클라이언트 중 하나의 자식 테마 functions.php 파일에 추가하고 있습니다.제품 종류에 따라 사용자 정의 woocommerce 카트 추가 요금이 있습니다. (non 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 고맙습니다. 완벽하게 작동했습니다. Tony –