fix(wpml): correct product permalinks for WPML translated products#819
Open
faisalahammad wants to merge 1 commit intoibericode:mainfrom
Open
fix(wpml): correct product permalinks for WPML translated products#819faisalahammad wants to merge 1 commit intoibericode:mainfrom
faisalahammad wants to merge 1 commit intoibericode:mainfrom
Conversation
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 ibericode#775
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 Summary
Integrates WPML-compatible product permalink generation directly into the plugin core, so WooCommerce product URLs synced to Mailchimp via the ecommerce module automatically resolve to the correct translated language version.
📋 Issue Reference
Fixes #775
🔍 Problem Description
Current Behavior
When using WooCommerce + WPML with Mailchimp's ecommerce features, product URLs synced to Mailchimp for non-default languages are incorrect. For example, a Romanian product might generate a URL like
celestekgodwin.com/ro/produs-slugbut with the English slug fragment mixed in, resulting in a broken link that opens the English product instead of the Romanian one.Expected Behavior
Product links in Mailchimp emails should open the correct translated version of the product, matching the language in which the product was selected.
Root Cause
As identified by @arnelap in the issue comments:
The ecommerce transformer calls
get_permalink()without switching WPML's language context first, so WPML cannot determine which language version of the URL to generate. This results in mixed-language URLs or outright default-language fallbacks.✨ Solution Overview
Approach Taken
Added two filter callbacks that hook into the existing ecommerce product data filters (
mc4wp_ecommerce_product_dataandmc4wp_ecommerce_product_variants_data) to correct the product URLs after they are generated but before they are sent to Mailchimp.Each callback:
ICL_SITEPRESS_VERSIONconstantwpml_element_language_codefilterwpml_permalinkfilterWhy This Approach
wpml_permalinkandwpml_element_language_code) as recommended by WPML documentationICL_SITEPRESS_VERSIONguard ensures the callbacks are complete no-ops when WPML is not installedsample-code-snippets/premium/ecommerce/wpml-product-permalinks.phpAlternatives Considered
🔧 Changes Made
Files Modified
includes/integrations/wpml-ecommerce.phpautoload.phprequirefor the new fileincludes/default-filters.phpDetailed Changes
1.
includes/integrations/wpml-ecommerce.php(New File)Why This Works:
WPML provides two official filter hooks for language-aware URL generation:
wpml_element_language_code: Returns the language code assigned to a specific post/product (e.g.,'ro'for Romanian)wpml_permalink: Converts a URL to the version for a given language, applying the correct language directory prefix (e.g.,/ro/) and translated slugBy chaining these two filters, we first determine which language a product belongs to, then transform its URL to match that language — exactly what WPML expects.
Impact:
2.
autoload.phprequire __DIR__ . '/includes/integrations/functions.php'; +require __DIR__ . '/includes/integrations/wpml-ecommerce.php';Added alongside the existing function
requirestatements since these are standalone functions, not class methods (consistent with howfunctions.phpand other function files are loaded).3.
includes/default-filters.phpmc4wp_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');Follows the established pattern in this file of registering filter callbacks.
🧪 Testing Performed
Automated Testing
$ vendor/bin/phpunit Welcome to the Mailchimp for WordPress Test Suite PHPUnit 9.6.34 by Sebastian Bergmann and contributors. ...................................................... 54 / 54 (100%) Time: 00:00.021, Memory: 6.00 MB OK (54 tests, 175 assertions)✅ All 54 tests pass with 175 assertions — no regressions introduced.
Regression Testing
ICL_SITEPRESS_VERSIONcheck, so they are no-ops in non-WPML environmentsapply_filters()mock intests/mock.phpreturns the value unchanged, confirming the functions work correctly in the test harness📊 Performance Impact
Analysis: Negligible
defined()check and return immediately — zero overheadapply_filters()calls per product sync, which is negligible compared to the HTTP API call to Mailchimp that followsNo additional database queries are introduced by this change.
🔒 Security Considerations
♿ Accessibility
N/A — Backend data transformation only, no UI changes.
🌍 Internationalization
✅ No breaking changes — Fully backward compatible.
sample-code-snippets/premium/ecommerce/wpml-product-permalinks.php) will continue to work alongside this fix (the filters will apply twice butwpml_permalinkis idempotent)✅ PR Checklist
🤝 Additional Context
sample-code-snippets/premium/ecommerce/wpml-product-permalinks.phpReady for Review ✨