I'm looking to mass remove or clear the default attribute for variable products in Woocommerce.
Is there an easy way to mass clear this value? If not, would you be able to direct me on where I can find this value in phpMyAdmin?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityDefault attributes of WooCommerce variable products are stored as post meta in the database.
You can find them in the wp_postmeta
table, where the post_id
column is the post ID of the parent product (Variable product), and the meta_key
column is _default_attributes
.
You can clear and remove default attributes of all products by replacing all non-empty arrays.
To do so, open phpMyAdmin and select the database of your WordPress installation from the left panel and click on SQL tab. Then write the below SQL commands and press Go:
Don't forget to backing up your database before executing any command on phpMyAdmin
UPDATE `wp_postmeta` SET `meta_value`= 'a:0:{}' WHERE meta_key = '_default_attributes'
you can modify the default attribute with the following code. this code retrieve the complet list of products then it can use a big amount of ressources
$products = wc_get_products([
"nopaging" => TRUE, // retrieve all products
]);
foreach ($products as $p) {
$default_attributes = $p->get_default_attributes();
if ( (!empty($default_attributes))
&& FALSE // other condition to select of which product the default attributes is reset
) {
$p->set_default_attributes("");
}
}