• Resolved Sallar Rabiei

    (@sallarrabiei)


    I face a problem in woocommerce shipping class. To explain it better I describe it with an example:

    • Product A has Air Shipping class
    • Product B has Road Shipping class
    • product C has Possible shipping in road and air
    • I use the following code to split the order based on shipping class:

      add_filter( 'woocommerce_cart_shipping_packages', 'wf_split_cart_by_shipping_class_group' );
      function wf_split_cart_by_shipping_class_group($packages){
          //Reset packages
          $packages               = array();
          
          //Init splitted package
          $splitted_packages      =   array();
          
          // Group of shipping class ids
          $class_groups =  array(
              'group1'    => array('plane'),
              'group2'    =>  array('truck'),
              // 'group3' =>  array(11,15,17),        
          );  
          
          foreach ( WC()->cart->get_cart() as $item_key => $item ) {
              if ( $item['data']->needs_shipping() ) {
                  
                  $belongs_to_class_group =   'none';
                  
                  $item_ship_class_id =   $item['data']->get_shipping_class();
                  
                  if($item_ship_class_id){
                      
                      foreach($class_groups as $class_group_key   =>  $class_group){
                          if(in_array($item_ship_class_id, $class_group)){                
                              $belongs_to_class_group = $class_group_key;
                              continue;
                          }
                      }
                      
                  }           
                  
                  $splitted_packages[$belongs_to_class_group][$item_key]  =   $item;
              }
          }
          
          // Add grouped items as packages 
          if(is_array($splitted_packages)){
              
              foreach($splitted_packages as $splitted_package_items){
                  $packages[] = array(
                      'contents'        => $splitted_package_items,
                      'contents_cost'   => array_sum( wp_list_pluck( $splitted_package_items, 'line_total' ) ),
                      'applied_coupons' => WC()->cart->get_applied_coupons(),
                      'user'            => array(
                           'ID' => get_current_user_id(),
                      ),
                      '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;
      }

      in this code plan class and truck class separated. but in the case of product number 3, user should select between air and road, since it is optional. But, when user selects road in the total price the shipping cost apply twice, while I expect to add the weight to the first line and just calculate the shipping class once. Is it any solution to handle this problem?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support RK a11n

    (@riaanknoetze)

    Hi there,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Split card when different products with different shipping classes are added’ is closed to new replies.