Description:
This guide explains how to include WPML language codes in the export file of User-Specific Pricing (USP) rules for products in a WooCommerce store.
If you’re using WPML for a multilingual setup, you might want to identify product language variations in the exported CSP rules file.
This tutorial will help you add the language code column and populate it with product-specific language information.
Steps to Include WPML Language Codes in the CSP Export File:
- Add a New Column for Language Code in the Export File:
- You will need to modify the export headers by adding a column for language codes. This can be done using the
wdm_csp_filter_product_usp_export_headers
filter. - Add the following code to the
functions.php
file of your child theme:
add_filter('wdm_csp_filter_product_usp_export_headers', 'newHeaderForUSPProductExportFile', 2, 11);
if (!function_exists('newHeaderForUSPProductExportFile')) {
/**
* Adds additional header in the export file for USP rules for products.
*
* @param array $existingHeaders
* @param string $exportType product_id|sku
* @return array
*/
function newHeaderForUSPProductExportFile( $existingHeaders, $exportType) {
$existingHeaders[] = 'Language Code';
return $existingHeaders;
}
}
2. Retrieve WPML Language Code for Each Product:
- Use the WPML filter
wpml_post_language_details
to fetch the language code for a specific product based on its product ID. - You will then need to apply this filter to get the language code for each product in the export rows.
- Add the following code to
functions.php
:
add_filter('wdm_csp_filter_product_usp_export_fields', 'newValuesInUSPProductExportFile', 2, 11);
if (!function_exists('newValuesInUSPProductExportFile')) {
/**
* Adds extra field value to the entries in the USP export file for products
*
* @param array $formatedResult associative array of usp rule fields
* @param string $exportType
* @return array
*/
function newValuesInUSPProductExportFile( $formatedResult, $exportType) {
$newResult = array();
foreach ($formatedResult as $ruleDetails) {
// Get language code for the product based on its ID
$ruleDetails->language_code = apply_filters('wpml_post_language_details', null, $ruleDetails->product_id);
$newResult[] = $ruleDetails;
}
return $newResult;
}
}
3. Export the Rules with Language Codes:
- After implementing the above code, when you export the User-Specific Pricing rules, the export file will include a new “Language Code” column.
- This column will display the corresponding language code for each product based on its product ID.
4. Remove Unnecessary Columns Before Importing Rules:
- If you’ve added additional columns (such as SKU or language codes), be sure to delete them from the export file before importing the rules back into your system. This ensures that only necessary data is imported correctly.
5. Optional: Add SKU or Other Fields to the Export:
- You can also add SKU or other custom fields to the export file by using the same filter method (
wdm_csp_filter_product_usp_export_headers
) with a higher priority. - Refer to the article on How to Include Additional Columns in the Rule Export File for detailed instructions.