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 thewpml_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 activatingflat_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 deactivatingflat_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 aforeach
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
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.