I'm using Hide shipping methods for specific shipping class in WooCommerce answer.
I use WPML plugin and I have two languages on my site, the piece of code is supposed hide certain shipping methods when there are products of a certain shipping class in the cart, and it does this perfectly, but only when the visitor is using the main language of the site, when using the secondary/translated version nothing happens...
I have added and modified functions in functions.php before, but this is the first time i've encountered this and don't understand why, so maybe someone has some insights as to what might be going on
UPDATE
After asking on the wpml forum, they sugested I use the wpml_object_id()
filter, which i did and that worked when only hiding one shipping method, so, this works:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
$shipping_class = apply_filters( 'wpml_object_id', 1, 'product_shipping_class', TRUE );
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class to find
$class = $shipping_class;
// HERE define the shipping method to hide
$method_key_id = 'flat_rate:1';
// Checking in cart items
foreach( $package['contents'] as $item ){
// If we find the shipping class
if( $item['data']->get_shipping_class_id() == $class ){
unset($rates[$method_key_id]); // Remove the targeted method
break; // Stop the loop
}
}
return $rates;
}
but when i change $method_key_id = 'flat_rate:1';
for $method_key_id = array('flat_rate:1','flat_rate:2');
to hide multiple shipping methods, i get some really buggy behavior, it seems to work if i add it before activating flat_rate:2
in shipping settings, but as soon as i activate it, it stops working entirely, and deactivating it again doesnt fix it, the only whay to fix it is deactivating flat_rate:2
and rolling back to using $method_key_id = 'flat_rate:1';
, in the linked question the array method is given as an option and i see multiple comments saying that it works, i must be missing something, but can't see it..
update 2
just noticed what i was missing, in the linked question there was a foreach
loop to go through the array, i hadn't noticed that, added it and it works now, this is my final code:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
$shipping_class = apply_filters( 'wpml_object_id', 1, 'product_shipping_class', TRUE );
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class to find
$class = $shipping_class;
// HERE define the shipping methods you want to hide
$method_key_ids = array('flat_rate:1', 'flat_rate:2');
// Checking in cart items
foreach( $package['contents'] as $item ) {
// If we find the shipping class
if( $item['data']->get_shipping_class_id() == $class ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
break; // Stop the loop
}
}
return $rates;
}
thanks everyone for the help