How to include WPML language codes in the export file of CSP rules.

Are you using a WPML for the multilingual setup of your e-commerce site along with CSP, and require WPML language codes in the rule export files?

  • WPML creates multiple copies of the Woocommerce product in order to allow users to set products for different languages.
  • All such copies will have different product Ids & attributes.
  • All such product copies will have the same SKU.
  • Hence, If CSP rules are applied to these products it’s not possible to identify the language of the products.

It is necessary to include WPML language codes in the CSP export file in order to identify the products’ languages.
This article explains how to include language codes in the CSP export file.

Let’s see how to include WPML language codes in the CSP export file of the User Specific Pricing rules

Include WPML language codes in the CSP export file.

Add a new column in the headers using the filter ‘wdm_csp_filter_product_usp_export_headers ‘.
you have to 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;
    }
}

Use the following code in order to get the language code of the product based on the product Id using the WPML filter.

apply_filters( 'wpml_post_language_details', null, $group_product_result[$key]->product_id);

Include the found WPML Language code in each of the rule rows.

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();
        $productNameList = wdmGetAllProductsIdNamePairs();
        foreach ($formatedResult as $ruleDetails) {
            $ruleDetails->language_code = apply_filters( 'wpml_post_language_details', null, $ruleDetails->product_id);
            $newResult[] = $ruleDetails;
        }
        return $newResult;
    }
}

You can add the SKU fields to the file by using the same filters with greater priorities.
Refer to the article How to include additional columns in the rule export file.
please note, you have to remove/delete the additional columns added to the export files in order to successfully import the rules.

Updated on October 29, 2020

Was this article helpful?

Related Articles