2014-05-16 3 views
2

배송 클래스에 따라 배송 방법을 하나만 숨기려고합니다. 특정 클래스에 속한 제품을 선택했을 때 FedEx 야간 방법을 필수적으로 강요합니다.Woocommerce, 배송 클래스에 따른 배송 방법 숨기기

add_filter('woocommerce_available_shipping_methods', 'hide_shipping_based_on_class' , 10, 1); 

function check_cart_for_share() { 

// load the contents of the cart into an array. 
global $woocommerce; 
$cart = $woocommerce->cart->cart_contents; 

$found = false; 

// loop through the array looking for the tag you set. Switch to true if the tag is  found. 
foreach ($cart as $array_item) { 
$term_list = wp_get_post_terms($array_item['product_id'], 'product_shipping_class', array("fields" => "names")); 

if (in_array("Frozen",$term_list)) { 

     $found = true; 
     break; 
    } 
} 

return $found; 

} 

function hide_shipping_based_on_class($available_methods) { 

// use the function above to check the cart for the tag. 
if (check_cart_for_share()) { 

// remove the rate you want 
unset($available_methods['canada_post,purolator,fedex:FEDEX_GROUND,fedex:GOUND_HOME_DELIVERY']); 
} 

// return the available methods without the one you unset. 
return $available_methods; 

} 

그것은 비록 어떤 배송 방법을 숨기는 것 같지 않습니다 아래에 표시된대로

나는 this code로 시작하고, 수정하고 있습니다. 아니 ... 확실히 내가 무엇을 누락

그것의 멀티 사이트 설치, 나는뿐만 아니라, 나는 테이블 비율 운송 모듈을 실행하는거야 http://stjeans.harbourcitydevelopment.com

에서 캐나다 측에서 테스트하고 있습니다 FedEx, Purolator 및 캐나다 포스트 모듈.

답변

3

동일한 문제가 발생하여 코드 수정에 도움이되었습니다. 하나의 문제는 "woocommerce_available_shipping_methods"필터가 WooCommerce 2.1에서 더 이상 사용되지 않습니다. 그래서 새로운 것을 사용해야합니다 : "woocommerce_package_rates". 유사한 업무를위한 WooCommerce tutorial가, 너무있다.

그래서 필터 후크를 변경하고 조건이 true 일 때 모든 배송 방법/비율을 반복하고 고객에게 표시하려는 항목을 찾은 다음 새 배열을 만들고이 배열을 반환합니다 (단 하나의 항목 만 포함).).

나는 당신의 문제가 주로 잘못된 unset ($ available_methods [...]) 행에 (deprecated 후크 옆에) 있다고 생각합니다. 그것은 그렇게 일할 수 없었다.

add_filter('woocommerce_package_rates', 'hide_shipping_based_on_class' , 10, 2); 
function hide_shipping_based_on_class($available_methods) { 
    if (check_cart_for_share()) { 
     foreach($available_methods as $key=>$method) { 
      if(strpos($key,'YOUR_METHOD_KEY') !== FALSE) { 
       $new_rates = array(); 
       $new_rates[$key] = $method; 
       return $new_rates; 
      } 
     } 
    } 
    return $available_methods; 
} 

경고 :

그래서 여기 내 코드입니다! woocommerce_package_rates hook 이 매번에 해고되는 것이 아니라 장바구니에있는 항목이나 수량을 변경할 때만 알게되었습니다. 또는 그것은 저를 위해 그것을 본다. 아마도 사용 가능한 요금이 카트 내용에 대해 어떻게 든 캐시됩니다.

0

벨로우즈 스 니펫을 사용하면 배송 등급에 따라 배송 방법을 숨길 수 있습니다. 자세한 설명을 참조하십시오. here

add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2); 

function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package) 
{ 
$hide_when_shipping_class_exist = array(
    42 => array(
     'free_shipping' 
    ) 
); 

$hide_when_shipping_class_not_exist = array(
    42 => array(
     'wf_shipping_ups:03', 
     'wf_shipping_ups:02', 
     'wf_shipping_ups:01' 
    ), 
    43 => array(
     'free_shipping' 
    ) 
); 


$shipping_class_in_cart = array(); 
foreach(WC()->cart->cart_contents as $key => $values) { 
    $shipping_class_in_cart[] = $values['data']->get_shipping_class_id(); 
} 

foreach($hide_when_shipping_class_exist as $class_id => $methods) { 
    if(in_array($class_id, $shipping_class_in_cart)){ 
     foreach($methods as & $current_method) { 
      unset($available_shipping_methods[$current_method]); 
     } 
    } 
} 
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) { 
    if(!in_array($class_id, $shipping_class_in_cart)){ 
     foreach($methods as & $current_method) { 
      unset($available_shipping_methods[$current_method]); 
     } 
    } 
} 
return $available_shipping_methods; 
}