Skip Navigation

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.

This topic contains 4 replies, has 2 voices.

Last updated by Shekhar Bhandari 1 year, 5 months ago.

Assisted by: Shekhar Bhandari.

Author Posts
September 13, 2023 at 12:39 pm #14390263

witoldR

Tell us what you are trying to do?
I'm trying to fix the hreflang on product variation. The website we're working with create custom functionality to generate the hreflang for product variation which expect the variation title to follow certain convention. However when the convention is not follow, the hreflang URL points to an 404.

I'm wondering if WPML generates the hreflang automatically for WooCommerce product variation.

Is there any documentation that you are following?
No.

Is there a similar example that we can see?

What is the link to your site?
hidden link

September 13, 2023 at 12:41 pm #14390275

witoldR

Could you include, include desar.***@***.nl in the support ticket so that I (Desar) get the notification when the ticket is updated?

Thank you.

Best regards,

Desar,
for Leapforce

September 18, 2023 at 5:14 am #14409061

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Welcome to WPML support, I'd be happy to help you on this.

I'm wondering if WPML generates the hreflang automatically for WooCommerce product variation.

I don't think hreflang is generated for WooCommmerce product variation automatically, but since you mentioned that you have added a code to generate it, possibly it's generating due to that code.

Can you remove the code and see if the hreflangs are generated?

Also, can you provide us the code that you are using?

Further, We recently released a new version of WPML plugins so can you please update the plugins to the latest version and let me know if that helps.

We push out WPML releases gradually. If your sites haven’t received this update yet, give it some time and you’ll see the update in a few days.

To skip the queue and get the update right away, to the Plugins → Add New page, click the Commercial tab and then click the Check for updates button.
https://wpml.org/wp-content/uploads/2020/04/wpml-force-plugin-update-1.png

Note: Keep a Backup Before Updating

It's impossible to add email notifications on the forum ticket, so I suggest checking the forum ticket itself for new messages.

Look forward to your reply.

Thanks

September 19, 2023 at 2:25 pm #14420353

witoldR

Hi there,

The website uses a normal page as a placeholder to display the product variation.
So when I removed the code, it display this page's hreflang like in the screenshot.

This is the code that is being used to 'fix' this hreflang

function translate_uri($uri) {
$uriBits = explode('/', $uri);

if ($uriBits[1] == 'en') {
$uriBits[2] = 'reparaties';

switch ($uriBits[4]) {
case "screen-repair":
$uriBits[4] = "scherm-reparatie";
break;
case "unlocken":
$uriBits[4] = "unlock";
break;
case "waterdamage-cleaning":
$uriBits[4] = "waterschade-schoonmaak";
break;
case "backcover-en":
$uriBits[4] = "backcover-vervangen";
break;
case "battery-replacement":
$uriBits[4] = "batterij-vervangen";
break;
case "camera-glass-repair":
$uriBits[4] = "camera-glaasje-reparatie";
break;
case "software-update":
$uriBits[4] = "updaten";
break;
default:
if (stripos($uriBits[4], "-repair")) {
$uriBits[4] = str_replace("-repair", "-reparatie", $uriBits[4]);
} else {
$uriBits[4] = str_replace("-en", "", $uriBits[4]);
}
break;
}

return "/$uriBits[2]/$uriBits[3]/$uriBits[4]/";
} else {
$uriBits[1] = 'repairs';

switch ($uriBits[3]) {
case "scherm-reparatie":
$uriBits[3] = "screen-repair";
break;
case "unlock":
$uriBits[3] = "unlocken";
break;
case "waterschade-schoonmaak":
$uriBits[3] = "waterdamage-cleaning";
break;
case "backcover-vervangen":
$uriBits[3] = "backcover-en";
break;
case "batterij-vervangen":
$uriBits[3] = "battery-replacement";
break;
case "camera-glaasje-reparatie":
$uriBits[3] = "camera-glass-repair";
break;
case "updaten":
$uriBits[3] = "software-update";
break;
default:
if (stripos($uriBits[3], "-reparatie")) {
$uriBits[3] = str_replace("-reparatie", "-repair", $uriBits[3]);
} else {
$uriBits[3] .= "-en";
}
break;
}

return "/en/$uriBits[1]/$uriBits[2]/$uriBits[3]";
}
}

function fix_hreflangs($hreflang_items) {

foreach ($hreflang_items as $key => $url) {
foreach (explode("/", $url) as $item) {
if ($item == 'product-variatie') {
if (explode('/', $_SERVER['REQUEST_URI'])[1] == 'en') {
$hreflang_items = array(
"nl-nl" => (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]" . translate_uri($_SERVER['REQUEST_URI']),
"en-us" => (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]",
);
} else {
$hreflang_items = array(
"nl-nl" => (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]",
"en-us" => (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]" . translate_uri($_SERVER['REQUEST_URI']) . '/',
);
}

return $hreflang_items;
}
}
}

return $hreflang_items;
}

add_filter('wpml_hreflangs', 'fix_hreflangs', 20);

Is there a way to get the id of the translated variation?
Say we have a Scherm Reparatie variation of iPhone 14, how do we get the translated variation of this, which would be Screen Repair of the same product?

Looking forward to hearing from you soon.

Best regards,

Desar,
for Leapforce.

Screenshot 2023-09-19 at 16.22.22.png
September 20, 2023 at 7:01 am #14426669

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

As the screenshot shows, the hreflangs aren't showing for product variation and only showing for the product, so it's the code.

Further, Is there a way to get the ID of the translated variation?

You can use these hooks, https://wpml.org/wpml-hook/wpml_object_id/

Look forward to your reply.

Thanks

September 26, 2023 at 9:39 am #14465463

witoldR

Thanks Shekhar,
I've updated the code to use the apply_filters

function fix_hreflangs($hreflang_items) {

foreach ($hreflang_items as $key => $url) {
foreach (explode("/", $url) as $item) {
if ($item == 'product-variatie') {
$variation = new VO_Variation();
$id = $variation->getVariationId();
$en = apply_filters('wpml_object_id', $id, 'product', false, 'en');
$nl = apply_filters('wpml_object_id', $id, 'product', false, 'nl');
$permalink_en = get_permalink($en);
$permalink_nl = get_permalink($nl);
$slug_en = wc_get_product_variation_attributes($en);
$slug_en = array_shift($slug_en);
$slug_nl = wc_get_product_variation_attributes($nl);
$slug_nl = array_shift($slug_nl);
$url_en = parse_url($permalink_en);
$url_nl = parse_url($permalink_nl);
$hreflang_en = $url_en['scheme'] . '://' . $url_en['host'] . $url_en['path'] . $slug_en . '/';
$hreflang_nl = $url_nl['scheme'] . '://' . $url_nl['host'] . $url_nl['path'] . $slug_nl . '/';

$hreflang_items = array(
"nl-nl" => $hreflang_nl,
"en-us" => $hreflang_en,
);

return $hreflang_items;
}
}
}

return $hreflang_items;
}

add_filter('wpml_hreflangs', 'fix_hreflangs', 20);