0

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:2and 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

Nook
  • 17
  • 4
  • 2
    **You need to ask WPML support** as shipping classes have certainly a different term Id for each translated language… Note that WooCommerce shipping classes are set as a custom taxonomy. – LoicTheAztec Dec 09 '20 at 17:27
  • hi, thanks! I'll do go do that, i used the inspect tool as you described in your answer on the other question and compared the class id and method id in both languages and they showed the same, so i assumed it was something else – Nook Dec 09 '20 at 17:47
  • Can you share relevant parts of your code? Maybe it's a matter of putting the term ID through a filter. – montrealist Dec 10 '20 at 22:39
  • I'm using the exact code as in the answer to the linked, only changing the term ID to what the ID's are on my site, i've asked the question on the wpml forum and they sugested using the wpml_object_id() filter, so I'm working on that right now – Nook Dec 12 '20 at 13:59

0 Answers0