2016-08-13 6 views
1

Based on this answer (code below)을 숨겨 사용할 수, 나는 성공적으로 특정 제품 카테고리로컬 배달 옵션에서 고정 요금을 숨길 수없는 지역 픽업 옵션을 사용할 수 있습니다. 이것은 완벽하게 작동합니다.배송 방법 - 고정 요금이

문제 :지역 픽업 옵션은 특정 범주에 대한 사용할 수 없습니다.

로컬 픽업 옵션을이 특수한 범주에서 사용하려면 어떻게해야합니까?

내가 사용하는 코드입니다 :

function custom_shipping_methods($rates){ 

    // 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) { 
     $item = $values['data']; 

     if (has_term($cat_slug, 'product_cat', $item->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; 
} 
add_filter('woocommerce_package_rates', 'custom_shipping_methods', 100); 

한가지 더 :이 보여줄 수 있나요 로컬 배달과 위치에 따라 특별한 범주에 대한 지역 픽업?

현재 내 상점 로컬 픽업 또는 배송은 하나의 위치에서만 설정됩니다.

답변

1

조언 :만 WooCommerce의 2.6.x 버전 (WC에 대한 추가 호환성 3 이상) 당신은 당신의 코드에서이 작은 일들을 변경해야합니다 ... 많은 테스트 후

에 대한 :

    :
    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 = 'posters'; 
        $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 $rate_id => $rate) { // <== There was a mistake here 
    
          if ('free_shipping' === $rate->method_id || 'local_pickup' === $rate->method_id || 'local_delivery' === $rate->method_id) { 
           $rates_arr[ $rate_id ] = $rate; 
           // break; // <========= Removed this to avoid stoping the loop 
          } 
         } 
        } 
        return !empty($rates_arr) ? $rates_arr : $rates; 
    } 
    

    2 실수가 있었다

  • 하나의 foreach 루프에 잘못된 변수 이름이 포함되어 있습니다.
  • break;을 제거하면 하나의 조건이 일치 할 때 foreach 루프가 중지되지 않습니다.

이 코드는 활성 자식 테마 또는 테마의 function.php 파일에 간다.

이 코드는 테스트(정확하게 배송 구역을 설정 한 경우 작동합니다) 완벽하게 작동한다.

운송료는 사용자가 캐시 된 데이터를 새로 고침해야합니다 해제, woocommerce 운송 설정에서 저장하고 활성화 현재의 운송 영역에 대한 관련 배송 방법을 저장합니다.


참고 :

+0

좋아요. 고맙습니다. 그것은 완벽하게 일했습니다 :) –

+0

@ ZahidIqbal 어리석은 실수와'휴식;'... 주변에서 보자 :) ... – LoicTheAztec