Knowledge Base

Table of Contents

Updated on May 16, 2023

Using PHP for Mapping Product Categories

Requirement

When importing/migrating products from one Woocommerce website to another we need to map categories in Website A to a category in Website B

For example, Website A has “Category X” and we want to map that to “Category Y” on Website B (target website).

Solution

The category on the target site must be setup first (Category Y) so that it get assigned on the import. Add this code to the Functions Editor on the Import file in WP All Import on target site.

Change ‘Translate this’ to ‘Category X’ and ‘To this’ to ‘Category Y’

If Mapping multiple categories fill int he next lines as above.

NB! The last rule must not have a comma. See no comma after => ‘Array’ so if only using one rule, don’t have a comma.

// Update the array with your mapping rules.
// The values on the left will be translated
// to the value on the right.
//
// Usage: [my_map_data({category[1]})]

function my_map_data( $data ) {
    $map = array(
        'Translate this'            => 'To this',
        'Add as many mapping rules' => 'As needed',
        'In this'                   => 'Array'
    );
    return ( array_key_exists( $data, $map ) ) ? $map[ $data ] : $data;
}