This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Bruno Kos 1 month, 3 weeks ago.

Assigned support staff: Bruno Kos.

Author Posts
August 10, 2019 at 9:08 am #4375031

martH

Tell us what you are trying to do? I use customized code in Functions.php and I want this affects in all languages, but now it works only in main language. Can I send you the code, so you can look it and suggest me some changes? I really need help with this!
Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

August 12, 2019 at 7:50 am #4378979

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Thank you for contacting WPML support!

I use customized code in Functions.php

Can you show us the code and tell us what it is supposed to do? Usually the custom code is outside of the scope of WPML support and we tell clients to reach out to our contractors, but perhaps we can help you:
https://wpml.org/contractors/

On a side note, you may want to check this:
https://wpml.org/documentation/support/wpml-coding-api/#language-constants
https://wpml.org/forums/topic/if-icl_language_codeit/

Regards,
Bruno Kos

August 15, 2019 at 8:25 am #4399475

martH

Hello!
The code is here:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

// HERE define your shipping class to find
$class = 2937;

// HERE define the shipping methods you want to hide
$method_key_ids = array('smartpost_estonia', 'dpd_shops_ee');

// 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;
}

This code hides two shipping methods ('smartpost_estonia', 'dpd_shops_ee') when shipping class is choosed.
Now it works only with main language. How to make it work in all languages? Maybe you have some standard code snippet to use in cases like this?

Thank you!
Diana

August 19, 2019 at 6:31 am #4413685

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi Diana,

Examining, testing and providing fix for custom code is normally outside of what WPML support offers and does I'm afraid. This is when we send clients to consult our experienced contractors:

https://wpml.org/contractors/

I'm not sure though whether you have seen this code already?
https://stackoverflow.com/a/56407287/1587045

It seems like this is an exact quote as yours, but only updated. Your issue might be related to:

$classes = [3031, 3032];

Or perhaps defining IDs of translated shipping classes, and not only originals?

And as I mentioned before, you may also consider using language constants:
https://wpml.org/documentation/support/wpml-coding-api/#language-constants

In other words, functions loaded only in specific languages, based on the language code.

Can you also have a look at this answer, it may explain some things as well:
https://wpml.org/forums/topic/different-shipping-methods-in-different-countries/#post-1152050
https://wpml.org/forums/topic/different-shipping-rates-in-different-languages/#post-729253
https://wpml.org/forums/topic/shipping-methods-in-different-countries/#post-1312394

All these answers given by our supporters have one thing in common:

"You can restrict shipping methods only by country, not by language, as shipping methods are going to be controlled via Woocommerce and not WPML."

Regards,
Bruno Kos