Home > OS >  Woocommerce: Change shipping class based on cart items shipping class count
Woocommerce: Change shipping class based on cart items shipping class count

Time:06-27

I have 2 class and 2 zones
zone 1
--------------------------------------------
class41 shipping rate =[qty]*0 (free)
class51 shipping rate =[qty]*10
--------------------------------------------

zone 2
--------------------------------------------
class41 shipping rate =[qty]*0 (free)
class51 shipping rate =[qty]*12
--------------------------------------------
I want to calculate the shipping cost only for class 51 products.

In class51, put 6 items in one box.
I want to calculate the shipping cost by unit with 6 items.

No matter how many items belonging to class41 are purchased, the shipping fee is free.
Items belonging to class51 will cost $ 10 for every 6 items in Zone 1.
for example
class41 x 5… zone 1 Free, zone 2 Free
class41 x 2 class51 x 6… zone 1 $10, zone 2 $12
class41 x 2 class51 x 13… zone 1 $30, zone 2 $36

I tried the following, but the shipping cost does not change.

add_action( 'woocommerce_cart_shipping_total', 'change_shipping_cost', 11, 2 );
function change_shipping_cost( $total, $cart ) {
  if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  return;
  /* HERE define your shipping class to find */
  $class_cool = array(51);
  /* Checking in cart items */
  $found = false;
  $shipping_total = $item_qty = 0;
  foreach( WC()->cart->get_cart() as $cart_item ){
    $item_shipping_class_id = $cart_item['data']->get_shipping_class_id();
    if( in_array( $item_shipping_class_id, $class_cool) ){
      $found = true;  /* Target shipping class found */
      $subtotal_shipping = WC()->cart->get_shipping_total();
      $item_qty  = $cart_item['quantity'];
      $item_quantity = (intdiv($item_qty   5, 6));  
    }
  }
  if( $found ) {
  $total = wc_price( $cart->shipping_total / $item_qty * $item_quantity );
  }
  return  $totals;
}

CodePudding user response:

Was self resolved.

First, I set the shipping rate as follows.
zone 1
--------------------------------------------
class41 shipping rate =[qty]*0
class51 shipping rate =10
--------------------------------------------

zone 2
--------------------------------------------
class41 shipping rate =[qty]*0
class51 shipping rate =12
--------------------------------------------

Added to the theme's functions.php.

add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );
function woocommerce_package_rates( $rates ) {
  if ( ! WC()->cart->is_empty() ) {
   $class_cool = array(51);
   $found = false;
  $item_qty = 0;
  foreach( WC()->cart->get_cart() as $cart_item ){
    $item_shipping_class_id = $cart_item['data']->get_shipping_class_id();
   if( in_array( $item_shipping_class_id, $class_cool) ){
        $found = true;  /* Target shipping class found */
      $item_qty  = $cart_item['quantity'];
       $item_quantity = (intdiv($item_qty   5, 6));
     }
  }
 }
 if( $found ) {
   foreach($rates as $key => $rate ) {
   $rates[$key]->cost = $rates[$key]->cost * $item_quantity;
  }
}
 return $rates;
}

Thank you.

  • Related