How to include additional columns in the rule export file.

This article explains how to include additional columns in the CSP export files.

Additional columns in the rule export
CSP Pricing Rules Export Page

Lists of default columns in the CSP export files

The following are the lists of default columns in the CSP export files.
(USP – Customer Specific Pricing, RSP – Role Specific Pricing, GSP – Group Specific Pricing.)

  • USP Product Rules
    • By Product Ids. [Product id, User, Min Qty, Flat, %]
    • By Product SKUs. [Sku, User, Min Qty, Flat, %]
  • RSP Product Rules
    • By Product Ids. [Product id, Role, Min Qty, Flat, %]
    • By Product SKUs. [Sku, Role, Min Qty, Flat, %]
  • RSP Product Rules
    • By Product Ids. [Product id, Group Name, Min Qty, Flat, %]
    • By Product SKUs. [Sku, Group Name, Min Qty, Flat, %]
  • 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].
  • 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].

It is obvious that you may need to include additional columns in the CSP rule export. It can be done by adding a new header to the CSV file & adding data for the newly added header in the record rows.

The following are the filter hooks which can be used to include additional columns in the rule export with respect to the export types.

Filter hooks for product export file.

USP

wdm_csp_filter_product_usp_export_headers.
wdm_csp_filter_product_usp_export_fields.

RSP

wdm_csp_filter_product_rsp_export_headers.
wdm_csp_filter_product_rsp_export_fields.

GSP

wdm_csp_filter_product_gsp_export_headers.
wdm_csp_filter_product_gsp_export_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 October 29, 2020

Was this article helpful?

Related Articles