【问题标题】:Create a shipping method for free items on woocommerce在 woocommerce 上为免费物品创建运输方式
【发布时间】:2020-11-18 10:59:30
【问题描述】:

我希望根据购物车中的商品创建两个单独的运输集。 当购物车中的咖啡超过 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');

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    更新:我打算通过更改来修复它

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

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

    我认为它不会提供正确的数量,但它可以满足我的需求。如果有人对如何更好地编写此内容有任何建议,将不胜感激。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 2018-07-22
      • 2021-02-28
      相关资源
      最近更新 更多