shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

How to Include Additional Columns in the CSP Rule Export File

Description:

This guide explains how to add extra columns to the CSP (Customer Specific Pricing) export files. You’ll learn how to extend the default export file by adding new headers and data fields for your specific needs, whether it’s for USP, RSP, or GSP rules.

Additional columns in the rule export
CSP Pricing Rules Export Page

Steps to Include Additional Columns in the Rule Export File:

  1. Default Columns in CSP Export Files: The CSP export files contain default columns depending on the type of rules (USP, RSP, or GSP). Below is a breakdown of the default columns for each type:

    USP (Customer Specific Pricing) Product Rules:
    • By Product Ids: [Product ID, User, Min Qty, Flat, %]By Product SKUs: [SKU, User, Min Qty, Flat, %]

    RSP (Role Specific Pricing) Product Rules:
    • By Product Ids: [Product ID, Role, Min Qty, Flat, %]By Product SKUs: [SKU, Role, Min Qty, Flat, %]

    GSP (Group Specific Pricing) Product Rules:
    • By Product Ids: [Product ID, Group Name, Min Qty, Flat, %]By Product SKUs: [SKU, Group Name, Min Qty, Flat, %]

    Category Rules:
    • USP Category Rules: [User ID, Category Slug, Min Qty, Flat Price, % Discount]RSP Category Rules: [Role, Category Slug, Min Qty, Flat Price, % Discount]GSP Category Rules: [Group ID, Category Slug, Min Qty, Flat Price, % Discount]

    Global Discount Rules:
    • USP Global Discount Rules: [User ID, Min Qty, Flat Price, % Discount]
    • RSP Global Discount Rules: [Role, Min Qty, Flat Price, % Discount]
    • GSP Global Discount Rules: [Group ID, Min Qty, Flat Price, % Discount]
  2. Adding Additional Columns: If you want to include extra columns in the CSP export, follow these steps:
    • Add a new header to the CSV file.
    • Insert data for the new column in each of the export rows.
  3. Using Filter Hooks to Add Additional Columns: WordPress provides several filter hooks that you can use to modify the export file and include additional columns. Here are the relevant filter hooks based on the rule type:
    • For USP (Customer Specific Pricing) Export:
      • wdm_csp_filter_product_usp_export_headers: Modify headers.
      • wdm_csp_filter_product_usp_export_fields: Modify the data in rows.
    • For RSP (Role Specific Pricing) Export:
      • wdm_csp_filter_product_rsp_export_headers: Modify headers.
      • wdm_csp_filter_product_rsp_export_fields: Modify the data in rows.
    • For GSP (Group Specific Pricing) Export:
      • wdm_csp_filter_product_gsp_export_headers: Modify headers.
      • wdm_csp_filter_product_gsp_export_fields: Modify the data in rows.
  4. Implementing the Code: To use these filter hooks, you’ll need to add custom code to your theme’s functions.php file or a custom plugin to modify the export behavior. By doing this, you can tailor the export file to your specific needs, including additional columns or custom data fields.

Example: Including the product titles in the product export file.

add_filter('wdm_csp_filter_product_usp_export_headers', 'newHeaderForUSPProductExportFile', 2, 11);
add_filter('wdm_csp_filter_product_usp_export_fields', 'newValuesInUSPProductExportFile', 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[] = 'product_name';
        return $existingHeaders;
    }
}

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->product_name = isset($productNameList[$ruleDetails->product_id])?str_replace(',', '_', $productNameList[$ruleDetails->product_id]):'--';
            $newResult[] = $ruleDetails;
        }
        return $newResult;
    }
}

if (!function_exists('wdmGetAllProductsIdNamePairs')) {
    /**
     * Retrives all the simple & variable products' product IDs & Names
     * and returns an array with product Id as a key & product title as a value
     *
     * @return array $products  array of productId-Name pairs
     */
    function wdmGetAllProductsIdNamePairs() {
        global $wpdb;
        $parentVariations     = array();
        $parentVariationNames = array();
        $fullProductList      = array();

        $productsList =    $wpdb->get_results(
                                                'SELECT ID, post_title FROM ' . $wpdb->prefix . 
                                                'posts where post_type="product" AND 
                                                post_status IN ("draft", "publish", "pending")'
                                            );

        $variantsList = $wpdb->get_results(        
                                                'SELECT ID, post_parent FROM ' . $wpdb->prefix . 
                                                'posts where post_type="product_variation" AND 
                                                post_status IN ("private", "publish", "pending")'
                                            );

        if ($variantsList) {
            foreach ($variantsList as $variant) {
                $parent=$variant->post_parent;
                if (!in_array($parent, $parentVariations)) {
                    $parentVariations[]=$parent;
                }
            }
        }

        if ($productsList) {
            foreach ($productsList as $singleProduct) {
                $product_id = $singleProduct->ID;
                if (!empty($parentVariations) && in_array($product_id, $parentVariations)) {
                    $parentVariationNames[$product_id]=$singleProduct->post_title;
                } else {
                    $fullProductList[$product_id] = $singleProduct->post_title;
                }
            }

        }
        if ($variantsList) {
            foreach ($variantsList as $variant) {
                    $variableProduct=wc_get_product($variant->ID);
                    $attributes=$variableProduct->get_attributes();
                    $fullProductList[$variant->ID] = $parentVariationNames[$variant->post_parent] . '-' . implode('_', $attributes);
                    unset($variableProduct);
            }
        }
        asort($fullProductList);
        return $fullProductList;
    }
}

Filters for the category export file & global discount rule export files can be used in a similar way.

Updated on December 10, 2024
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Was this article helpful?

shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Related Articles