2016-08-12 9 views
1

woocommerce 배송 옵션에 도움이 필요합니다. 지역 배송 또는 현지 픽업 옵션 만 보여주고 싶은 특정 제품 카테고리의 고정 요금을 숨기려고합니다.배송 제거 WooCommerce 2.6 및 3+의 특정 카테고리에 대한 정액 요금제

다른 모든 카테고리의 경우 모든 옵션이 작동합니다.

나는 (내 테마의 function.php 파일에 추가) 그 코드로 수행하려고 있습니다

function cart_has_product_with_orange_cats() { 

    global $woocommerce; 
    $product_in_cart = false; 

    // start of the loop that fetches the cart items 
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
     $_product = $values['data']; 
     $terms = get_the_terms($_product->id, 'product_cat'); 
     // second level loop search, in case some items have several categories 

     if($terms){ 

      foreach ($terms as $term) { 
       $_categoryid = $term->term_id; 

       if ($_categoryid == 16) { 
        //category is in cart! 
        $product_in_cart = true; 
       } 
      } 
     } 
    } 
    return $product_in_cart; 
} 

// add filter and function to hide method 

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

function custom_shipping_methods($available_methods){ 

    if (cart_has_product_with_orange_cats()) { 

     foreach($available_methods as $key => $method){ 
      if($key == 'local_delivery' || $key == 'local_pickup'){ 
       continue; 
      } 
      unset($available_methods[$key]); 

     } 
     // remove the rate you want 
    } 
    // return the available methods without the one you unset. 
    return $available_methods; 
} 

그러나 그것은 나를 위해 일하고, 어쩌면 때문에 최신 WooCommerce 버전의 오래된 코드 또는하지 않습니다 다른 문제.

어떻게하면됩니까?

감사합니다.

답변

3

업데이트(화장실 추가 호환성 3 이상)

이 응답에 대한 코드 다른 thread에 위치하여 보정 테스트를 완벽하게 기능 :
Shipping methods - Local Pickup option not available when Flat Rate is hidden

중요 0

:은 WC 버전 2.1 이후 사용되지 않는 것입니다
woocommerce_available_shipping_methods 후크 대신 woocommerce_package_rates 필터 후크를 사용해야합니다.

WooCommerce 버전 2.6 이상 소개 신규 배송 구역. 따라서 WooCommerce 이전 버전의 배송비 관련 코드는 이며,은 더 이상 작동하지 않습니다.. 정보에 대한


: ** 대신 더 이상 global $woocommerce;을 필요로하지 않으며 대신해야합니다, 그래서 당신은 단지, WC()->cart 구문을 사용할 수 있습니다 $woocommerce->cart->get_cart()global $woocommerce;를 사용하여 WC()->cart->get_cart() ...

WordPress에 이미 존재하는 조건부 함수가 있으므로 WooCommerce 제품 범주를 대상으로 지정할 수 있으므로 사용자 지정 조건부 함수는 더 이상 필요하지 않습니다.

has_term('your_category_slug', 'product_cat', $item_id) 

조언 :이 기능은 용어 슬러그 대신 용어 ID의 사용 그래서 코드는 아래 만 WooCommerce 버전 2.6+에 대한 입니다 ...

을 우리 이 코드의 조건부 함수 has_term()을 사용할 수 있습니다.

배송 방법은 캐시로
add_filter('woocommerce_package_rates', 'custom_shipping_methods', 100, 2);  
function custom_shipping_methods($rates, $package){ 

    // Define/replace here your correct category slug (!) 
    $cat_slug = 'your_category_slug'; 
    $prod_cat = false; 

    // Going through each item in cart to see if there is anyone of your category   
    foreach (WC()->cart->get_cart() as $values) { 
     $product = $values['data']; 

     // compatibility with WC +3 
     $product_id = method_exists($product, 'get_id') ? $product->get_id() : $product->id; 

     if (has_term($cat_slug, 'product_cat', $product_id)) 
      $prod_cat = true; 
    } 

    $rates_arr = array(); 

    if ($prod_cat) { 
     foreach($rates as $key => $rate) { 
      if ('free_shipping' === $rate->method_id || 'local_pickup' === $rate->method_id || 'local_delivery' === $rate->method_id) { 
       $rates_arr[ $rate_id ] = $rate; 
       break; 
      } 
     } 
    } 
    return !empty($rates_arr) ? $rates_arr : $rates; 
} 

스 니펫를 추가하기 전에, 반드시 분명 당신의 WooCommerce 캐시 (WooCommerce> 시스템 상태> 도구> WC 과도> 지우기 과도)합니다.

이 코드는 활성 자녀 테마 또는 테마의 function.php 파일로 이동합니다.

+0

안녕, 당신의 멋진 대답, 그 작업에 대한 감사하지만, 단 하나의 문제는, 현지 픽업 옵션을 사용할 수 없습니다. 로컬 배달 만 표시합니다. 나는 로컬 픽업을 보여주고 싶다. 고맙습니다. –