Skip to content

Commit 9a59d6d

Browse files
add snippet for translating product permalink when using woocommerce + wpml, see #775
1 parent 1e5e568 commit 9a59d6d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* This snippet sets the product permalink to its translated version when using WooCommerce + WPML
5+
*
6+
* See https://github.com/ibericode/mailchimp-for-wordpress/issues/775
7+
*/
8+
9+
add_filter('mc4wp_ecommerce_product_data', function ($data, WC_Product $product) {
10+
$language = apply_filters('wpml_element_language_code', null, array(
11+
'element_id' => $data['id'],
12+
'element_type' => 'product'
13+
));
14+
$data['url'] = apply_filters('wpml_permalink', $data['url'], $language);
15+
return $data;
16+
}, 10, 2);
17+
18+
add_filter('mc4wp_ecommerce_product_variants_data', function ($variants, $product) {
19+
foreach ($variants as $key => $variant) {
20+
$language = apply_filters('wpml_element_language_code', null, array(
21+
'element_id' => $variant['id'],
22+
'element_type' => 'product'
23+
));
24+
$variants[$key]['url'] = apply_filters('wpml_permalink', $variant['url'], $language);
25+
}
26+
return $variants;
27+
}, 10, 2);

0 commit comments

Comments
 (0)