In a recent Business Bloomer Club Slack thread, a WooCommerce user needed to bulk edit product attributes across a large catalog.
Specifically, they wanted to modify only certain attributes, leaving others untouched, without relying on WP All Import due to timeout issues when dealing with a high volume of products.
This guide covers how to approach this type of bulk attribute modification using both database and code-based solutions while minimizing the risk of timeouts or errors.
Step 1: Understanding Attribute Storage in WooCommerce
WooCommerce stores product attribute data in the postmeta
table, specifically under the _product_attributes
meta key for each product. This data is stored as serialized data, making it trickier to edit directly because changing it improperly can corrupt the data format.
To locate product attribute data:
- Access the
wp_postmeta
table in your database. - Find the
post_id
of the product you wish to edit. - Look for the
_product_attributes
meta key, which contains serialized data, including fields likeis_variation
for each attribute.
Step 2: Bulk Update Serialized Data Using SQL Queries
For those comfortable with SQL, you can try using a REPLACE
function in MySQL. However, because serialized data is highly sensitive to structure changes, test these queries on a staging site first to avoid corruption.
Example SQL query to toggle is_variation
on certain attributes:
UPDATE wp_postmeta
SET meta_value = REPLACE(meta_value, 's:10:"is_variation";i:1;', 's:10:"is_variation";i:0;')
WHERE meta_key = '_product_attributes' AND post_id = [your_product_id];
Important: Modify only specific parts of the serialized string, and always test on a few records first. Direct SQL updates should only be attempted if you’re familiar with serialized data structures, as errors can break your product pages.
Step 3: Code-Based Solution for Selective Attribute Update
A safer approach is to use WooCommerce and WordPress hooks in PHP to loop through products and update only specific attributes. This method ensures that serialized data remains intact.
Here’s an example of how to do this:
function update_product_attributes_variations() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // Adjust for testing if necessary
);
$products = get_posts( $args );
foreach ( $products as $product_post ) {
$product = wc_get_product( $product_post->ID );
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute_name => $attribute ) {
if ( $attribute_name !== 'brand' && $attribute_name !== 'strength' ) {
$attribute->set_variation( false ); // Disable variation for non-targeted attributes
}
}
$product->save();
}
}
add_action( 'init', 'update_product_attributes_variations' );
- Explanation: This code loops through all products and updates only attributes that aren’t named “brand” or “strength,” disabling the
is_variation
field for the rest. Run this code on a staging site to verify that it behaves as expected before deploying.
Step 4: Overcoming WP All Import Timeout Issues
If you prefer using WP All Import, try these tips to prevent timeout issues:
- Limit Imports to Specific Fields: Update only the attributes field rather than the entire product record to reduce processing time.
- Adjust Timeouts and Batches: Increase PHP execution time on your server, or configure WP All Import to process smaller batches.
- Disable Unnecessary Plugins: Temporarily disable plugins other than WooCommerce and WP All Import during the import to reduce server load.
Conclusion
Updating attributes across a WooCommerce product catalog can be complex, especially with serialized data. For safe results, try using a PHP function to loop through products and adjust attributes selectively. Alternatively, adjust WP All Import settings to prevent timeouts. Always test these changes on a staging site first to prevent accidental data corruption on live products.