Here is a Python script that you can use to extend current product categories by parent categories of these categories.
To use it you need to:
- save this script into the extend_categories.py file
- download and install Python,
- install the pandas package by running this command in the terminal: pip install pandas
- export products from your store, rename the CSV file to products.csv, and place it in the same folder with an extend_categories.py file.
- run the script by the command in the terminal: python extend_categories.py
*In some cases, you may need to use pip3 and python3 instead of pip and python
It will add a column ExtendedCategory next to the Category column and save it into the modyfied_products.csv file.
Then you can check it and import it back to your store, choosing the ExtendedCategory column as a category column instead of the default Category column.
import pandas as pd
def extend_categories(categories: str):
if categories:
ext_categories = []
for category_path in categories.split(';'):
categories = category_path.split('/')
for i in range(1, len(categories) + 1):
path = '/'.join(categories[:i])
if path not in ext_categories:
ext_categories.append(path)
return ';'.join(ext_categories)
df = pd.read_csv("products.csv", encoding='utf-8', dtype=str)
ext_category = df['Category'].fillna('').map(extend_categories)
col_position = df.columns.get_loc('Category')
df.insert(col_position + 1, 'ExtendedCategory', ext_category)
df.to_csv("modified_products.csv", index=False, encoding='utf-8')
print('Done!')