The code checks if the shipping class is equal to id 53 and hides the shipping method. The problem is that: if someone adds more than one product, the code checks only in the last added item. I would like it to count all added products first and if there are more than 2 products with the same shipping class, then it will hide the "pack locker" shipping method Please help
`add_filter( 'woocommerce_package_rates', 'fanka_kodowania_hide_shipping_method1', 10, 2 );
function fanka_kodowania_hide_shipping_method1( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
/*****************KLASA WYSYŁKI**************************/
$class1 = 53;
/********************************************************/
/*****************METODY WYSYŁKI*************************/
$method_key_ids = array('inpost_paczkomaty:12');
/********************************************************/
global $woocommerce;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$item = $cart_item['data'];
if(!empty($item)) {
$quantity = $cart_item['quantity'];
}
}
foreach( $package['contents'] as $item ) {
if( $item['data']->get_shipping_class_id() == $class1 && $quantity >1 ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]);
}
}
}
return $rates;
}
`
CodePudding user response:
This check checks the last item in the cart when determining if the shipping method should be hidden.
Add a check for quantity:
add_filter( 'woocommerce_package_rates', 'fanka_kodowania_hide_shipping_method1', 10, 2 );
function fanka_kodowania_hide_shipping_method1( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Shipping class to check for
$class1 = 53;
// Shipping method to hide
$method_key_ids = array('inpost_paczkomaty:12');
// Keep track of the quantity of items with the specified shipping class
$quantity = 0;
foreach( $package['contents'] as $item ) {
if( $item['data']->get_shipping_class_id() == $class1 ){
$quantity = $item['quantity'];
}
}
// If there are more than two items with the specified shipping class, hide the shipping method
if( $quantity > 1 ) {
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]);
}
}
return $rates;
}
CodePudding user response:
The problem with the code is that it is only checking the quantity of the last item added to the cart, and not the total quantity of items with the shipping class id of 53. To fix this, you can add a variable to keep track of the total quantity of items with that shipping class id, and then check that variable's value at the end to determine whether to hide the shipping method.
add_filter( 'woocommerce_package_rates', 'fanka_kodowania_hide_shipping_method1', 10, 2 );
function fanka_kodowania_hide_shipping_method1( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
/*****************KLASA WYSYŁKI**************************/
$class1 = 53;
/********************************************************/
/*****************METODY WYSYŁKI*************************/
$method_key_ids = array('inpost_paczkomaty:12');
/********************************************************/
//create variable to keep track of total quantity of items with class1
$total_quantity = 0;
foreach( $package['contents'] as $item ) {
if( $item['data']->get_shipping_class_id() == $class1 ){
$total_quantity = $item['quantity'];
}
}
//check if total quantity is greater than 1
if( $total_quantity > 1 ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]);
}
}
return $rates;
}
This will iterate through the items in the cart, and for each item with the shipping class id of 53, it will add the quantity of that item to the total_quantity variable. After iterating through all items, it will check if the total quantity is greater than 1, and if so it will remove the specified shipping method from the available options.
Good luck !