From 8ed7bf5c9b43f7207104ccbca57ccadef705b761 Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Mon, 16 Feb 2026 21:39:11 +0600 Subject: [PATCH] fix(wpml): correct product permalinks for WPML translated products Add WPML compatibility for ecommerce product permalink generation. When products are synced to Mailchimp, the product URLs now include the correct WPML language context, ensuring translated products link to the correct language version. - Add wpml-ecommerce.php with permalink correction functions - Hook into mc4wp_ecommerce_product_data filter for products - Hook into mc4wp_ecommerce_product_variants_data for variants - Only applies when WPML (ICL_SITEPRESS_VERSION) is active Fixes #775 --- autoload.php | 1 + includes/default-filters.php | 4 ++ includes/integrations/wpml-ecommerce.php | 73 ++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 includes/integrations/wpml-ecommerce.php 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; +}