Importing Custom Headers for WooCommerce Products & Variations with WPSyncSheets for WooCommerce
You may have custom columns in Google Sheets that store additional data for WooCommerce product variations, such as internal reference codes, external system IDs, or custom attributes.
Using WPSyncSheets, you can import these values and save them as product or variation meta in WooCommerce.
The following steps explain how to register and import custom headers for products.
Step 1: Register the Custom Header for Product
Register the custom header so it is available for product import.
Headers must be returned as an associative array in the format:
meta_key => Column Label
add_filter( 'wpssw_custom_headers_for_product_import', function ( $headers ) {
$headers['_supplier_code'] = 'Product Supplier Code';
return $headers;
});
Step 2: Handle the Import Logic
Handle the import logic and store the imported value as product meta.
add_action(
'wpssw_custom_headers_for_product_doimport',
function ( $product_id, $header_label, $row_data, $header_value ) {
if ( $header_label === 'Product Supplier Code' ) {
update_post_meta( $product_id, '_supplier_code', $header_value );
}
},
10,
4
);
This action provides the following parameters to control how product custom headers are imported.
| Parameter | Description |
|---|---|
$product_id | The product ID |
$header_label | The column label from Google Sheets (not the meta key) |
$row_data | The entire Google Sheets row for the current product |
$ | The value of the current column being imported |
The following steps explain how to register and import custom headers for product variations.
Step 1: Register the Custom Header for Variation Product
Register the custom header so it is available for variation product import.
Headers must be returned as an associative array in the format:
meta_key => Column Label
add_filter( 'wpssw_headers_for_variation_product_import', function ( $headers ) {
$headers['variation_supplier_code'] = 'Variation Supplier Code';
return $headers;
});
Step 2: Apply the Header Only to Variations (Exclude Parent Product)
This filter registers the header exclusively for variation imports, so it is never applied to parent products.
add_filter( 'wpssw_custom_headers_for_variation_product_import', function ( $headers ) {
$headers['variation_supplier_code'] = 'Variation Supplier Code';
return $headers;
});
Step 3: Handle the Import Logic
Override the default import behavior and store the imported value as variation meta.
add_filter(
'wpssw_custom_headers_for_product_variation_doimport',
function ( $override, $product_id, $meta_key, $meta_value ) {
if ( $meta_key === 'variation_supplier_code' ) {
update_post_meta( $product_id, 'variation_supplier_code', $meta_value );
return true;
}
return $override;
},
10,
4
);
This filter provides the following parameters to control how variation custom headers are imported.
| Parameter | Description |
|---|---|
$override | Default value. Return a non-null value (e.g. true) to stop default handling |
$product_id | The variation product ID |
$meta_key | The meta key (header key) being imported |
$meta_value | The value from Google Sheets for this variation |