woocommerce에서 무료 항목에 대한 배송 방법 만들기

Nov 17 2020

장바구니에있는 항목을 기준으로 두 개의 별도 배송 세트를 만들려고합니다. 카트에 커피 봉지가 2 개 이상인 경우 배송 등급 "휴일 패키지"에 대해 무료 배송 및 제품 범주 "커피"에 대해 무료 배송을 원합니다. 그런 다음 나머지 품목을 다른 배송 "컨테이너"에 넣기를 원합니다.

나는 현재 거기에 두 개의 다른 커피가있을 때 모든 것을 작동시킬 수 있지만 (따라서 커피 수를 똑딱 거리고있다) 한 종류의 커피 중 두 가지가있을 때 그것을 분리하지 않습니다.

/* Puts items with "holiday-package" shipping class into a different shipping package. */
 function hdm_woocommerce_cart_shipping_packages( $packages ) { // Reset the packages $packages = array();

     // Free items
     $freeshipping_items = array(); $regular_items      = array();
     $coffee_count = 0; //get product category and count up each item in that category foreach ( WC()->cart->get_cart() as $item ) {
       $quantity = $item['quantity'];
      if ($item['data']->get_shipping_class() == 'coffee') { $coffee_count++; }
      elseif ($quantity >= 2 && $item['data']->get_shipping_class() == 'coffee') {
          $coffee_count = 2; } } // Sort free from regular. foreach( WC()->cart->get_cart() as $item ) {
         if( $item['data']->needs_shipping() ) { if( $item['data']->get_shipping_class() == 'holiday-packages' ) {
                 $freeshipping_items[] = $item;
             }
             elseif ($item['data']->get_shipping_class() == 'coffee' && $coffee_count >= 2){
               $freeshipping_items[] = $item;
             }
             else {
                 $regular_items[] = $item;
             }
         }
     }

     // Put inside packages:
     if( $regular_items ) { $packages[] = array(
             'ship_via'        => array(),
             'contents'        => $regular_items, 'contents_cost' => array_sum(wp_list_pluck($regular_items, 'line_total')),
             'applied_coupons' => WC()->cart->applied_coupons,
             'destination'     => array(
                 'country'   => WC()->customer->get_shipping_country(),
                 'state'     => WC()->customer->get_shipping_state(),
                 'postcode'  => WC()->customer->get_shipping_postcode(),
                 'city'      => WC()->customer->get_shipping_city(),
                 'address'   => WC()->customer->get_shipping_address(),
                 'address_2' => WC()->customer->get_shipping_address_2()
             )
         );
     }
     if( $freeshipping_items ) { $packages[] = array(
             'ship_via'        => array( 'flat_rate' ),
             'contents'        => $freeshipping_items, 'contents_cost' => array_sum(wp_list_pluck($freeshipping_items, 'line_total')),
             'applied_coupons' => WC()->cart->applied_coupons,
             'destination'     => array(
                 'country'   => WC()->customer->get_shipping_country(),
                 'state'     => WC()->customer->get_shipping_state(),
                 'postcode'  => WC()->customer->get_shipping_postcode(),
                 'city'      => WC()->customer->get_shipping_city(),
                 'address'   => WC()->customer->get_shipping_address(),
                 'address_2' => WC()->customer->get_shipping_address_2()
             )
         );
     }

     return $packages;
 }

 add_filter('woocommerce_cart_shipping_packages', 'hdm_woocommerce_cart_shipping_packages');

답변

MattNath Nov 18 2020 at 02:59

업데이트 : 변경하여 수정하려고했습니다.

  elseif ($quantity >= 2 && $item['data']->get_shipping_class() == 'coffee') {
      $coffee_count = 2;
      }

...에

  if ($quantity >= 2 && $item['data']->get_shipping_class() == 'coffee') { $coffee_count = 2;
      }

나는 그것이 정확한 양으로 나올 것이라고 생각하지 않지만 그것은 내 요구에 맞습니다. 누구든지 이것을 더 잘 작성하는 방법에 대한 조언이 있다면 대단히 감사하겠습니다.