Ebot Solutions : Website & Software Development Company in Chennai India

Ebot Solutions : Website & Software Development Company in Chennai India

Ebot Solutions : Website & Software Development Company in Chennai India

Effortlessly Create WooCommerce Product Variations with Python: A Step-by-Step Guide

WooCommerce Product variation Using Python Script

Managing product variations effectively is critical for e-commerce platforms. This Python script demonstrates how to process a CSV file of products, generate variations based on size, and save the enriched data back to a new file. Below is a breakdown of the script, step by step, to help you understand how it works.

1. Importing Required Libraries

The script begins by importing essential Python libraries:

 
import pandas as pd
import numpy as np
import os
  • pandas (pd): For data manipulation and handling CSV files.
  • numpy (np): Provides numerical operations, though it’s not directly used here.
  • os: Used to interact with the operating system, specifically for path management.

2. Setting Up File Paths

 

print(os.path.dirname(__file__))

filepath = "<input file Path>"
tgt = "<output file Path>"

  • os.path.dirname(__file__) prints the directory where the script is located.
  • filepath is the path to the input CSV file containing product data.
  • tgt is the path to the output CSV file where processed data will be saved.

3. Defining Size Variations and Prices

 

size = {'variation_values': ["small", "large", "medium"]}

standard_price = 100

small = standard_price
medium = standard_price + 20
large = standard_price + 40

  • The size dictionary contains three size categories: small, medium, and large.
  • A base price (standard_price) is defined, with price adjustments for larger sizes.

4. Reading the Input CSV

 
 
df_prod_var = pd.read_csv(filepath)
dfhrdconfig = df_prod_var.copy(deep=True)
  • df_prod_var loads the input product data from the CSV.
  • dfhrdconfig creates a deep copy of this DataFrame for later use.

5. Creating a DataFrame for Size Variations

 
 
df_size = pd.DataFrame(size)
  • Converts the size dictionary into a DataFrame for easy merging.

6. Merging Product Data with Size Variations

 
 
df_prod_var['key'] = 1
df_size['key'] = 1

 

dfMerge = pd.merge(df_prod_var, df_size, on='key')

  • A dummy column (key) is added to both DataFrames to facilitate a Cartesian join.
  • dfMerge combines each product with all size variations.

7. Preparing the Variations DataFrame

 
 
dfhrdconfig['Type'] = 'variable'
dfhrdconfig['Parent'] = ''
dfunion = pd.concat([dfhrdconfig, dfMerge], ignore_index=True)
  • dfhrdconfig is updated with:
    • Type: Indicates this is a variable product.
    • Parent: Empty column to store parent product information.
  • pd.concat merges the original and size-variation DataFrames.

8. Adding SKU and Position Information

 
 
dfunion.loc[dfunion.index > 0 , 'SKU'] = dfunion['SKU'] + '-VAR-' + dfunion.index.astype(str)
dfunion['Position'] = dfunion.index.astype(str)
  • For size variations (index > 0), a unique SKU is generated by appending the variation and index.
  • Position assigns a sequential index to each entry.

9. Assigning Attributes and Prices

 
 

dfunion.loc[dfunion.index > 0 , 'Attribute 1 value(s)'] = dfunion['variation_values']

dfunion.loc[(dfunion.index > 0 ) & (dfunion['variation_values'] == 'small'), 'Regular price'] = small
dfunion.loc[(dfunion.index > 0 ) & (dfunion['variation_values'] == 'medium'), 'Regular price'] = medium
dfunion.loc[(dfunion.index > 0 ) & (dfunion['variation_values'] == 'large'), 'Regular price'] = large

  • Attribute 1 value(s): Specifies size for each variation.
  • Regular price: Assigns prices based on the size category.

10. Adding Tax Class and Final Adjustments

 
 
dfunion.loc[(dfunion.index > 0 ), 'Tax class'] = 'parent'
dfunion.loc[(dfunion.index > 0 ), 'Type'] = 'variation'

 

dfunion = dfunion.drop('key', axis=1)
dfunion = dfunion.drop('variation_values', axis=1)

  • Tax class: Indicates the parent tax category for variations.
  • Type: Updated to variation for size-specific entries.
  • Drops intermediate columns (key and variation_values) used for processing.

11. Exporting the Final DataFrame

 
 
dfunion.to_csv(tgt)

The processed data is saved to the output file specified by tgt.


Key Outputs

The final CSV will include:

  • Original product entries (Type = “variable”).
  • Variations for each size with attributes like SKU, size, price, and tax class.
Comments are closed.