diff --git a/autoload.php b/autoload.php index e2751c05..6cc73eb8 100755 --- a/autoload.php +++ b/autoload.php @@ -5,6 +5,7 @@ require __DIR__ . '/includes/forms/functions.php'; require __DIR__ . '/includes/forms/admin-functions.php'; require __DIR__ . '/includes/integrations/functions.php'; +require __DIR__ . '/includes/integrations/wpml-ecommerce.php'; spl_autoload_register(function ($class) { static $classmap = [ diff --git a/includes/default-filters.php b/includes/default-filters.php index 860dec02..44663ba7 100755 --- a/includes/default-filters.php +++ b/includes/default-filters.php @@ -15,3 +15,7 @@ mc4wp_apply_deprecated_filters('mc4wp_merge_vars', 'mc4wp_form_data'); mc4wp_apply_deprecated_filters('mc4wp_form_merge_vars', 'mc4wp_form_data'); mc4wp_apply_deprecated_filters('mc4wp_integration_merge_vars', 'mc4wp_integration_data'); + +// WPML: fix product permalinks in ecommerce data synced to Mailchimp +add_filter('mc4wp_ecommerce_product_data', 'mc4wp_wpml_ecommerce_product_permalink'); +add_filter('mc4wp_ecommerce_product_variants_data', 'mc4wp_wpml_ecommerce_product_variants_permalink'); diff --git a/includes/integrations/wpml-ecommerce.php b/includes/integrations/wpml-ecommerce.php new file mode 100644 index 00000000..c2c9c63c --- /dev/null +++ b/includes/integrations/wpml-ecommerce.php @@ -0,0 +1,73 @@ + $data['id'], + 'element_type' => 'product', + ]); + + $data['url'] = apply_filters('wpml_permalink', $data['url'], $language); + + return $data; +} + +/** + * Fixes the product variant permalinks for WPML translated products. + * + * Hooks into the ecommerce product variants data filter to replace each + * variant URL with the correct WPML-translated permalink. + * + * @since 4.11.2 + * + * @param array $variants Array of variant data arrays, each containing 'id' and 'url' keys. + * @return array Modified variants data with corrected URLs. + */ +function mc4wp_wpml_ecommerce_product_variants_permalink($variants) +{ + if (!defined('ICL_SITEPRESS_VERSION')) { + return $variants; + } + + foreach ($variants as $key => $variant) { + $language = apply_filters('wpml_element_language_code', null, [ + 'element_id' => $variant['id'], + 'element_type' => 'product', + ]); + + $variants[$key]['url'] = apply_filters('wpml_permalink', $variant['url'], $language); + } + + return $variants; +}