I have a variable product with 5 variations (based on a single attribute – size).
So:
Product “Shirt” – product_id = 1234
- variation XS (extra small) – variation_id = 1235
- variation S (small) – variation_id = 1236
- variation M (medium) – variation_id = 1237
- variation L (large) – variation_id = 1238
- variation XL (extra large) – variation_id = 1239
I want to delete variation XL.
best I could find was (considering a variation is a post of ‘product_variation’ type):
<?php wc_delete_post(1239, true); ?>
This removes the variation and the associated metadata, however, I am still left with the size attribute in the list of possible attributes when editing product (in the attributes tab) see the attached pic.
Does anybody have a php code snipped that would fully remove a product variation including this attribute entry.
You can get rid of the attribute in the product with:
More here: https://codex.wordpress.org/Function_Reference/wp_remove_object_terms
WooCommerce 3.0+
To Delete a Variation (source code):
Auto-Removing Attributes from Variable Product
The Size values in your screenshot are linked to your Variable Product, and not the individual Variations.
Deleting a Variation will therefore not remove the associated Attributes from the parent product, as you still have the choice of making new Variations with those Attributes (the possibility of adding an XL shirt later on is still there).
However, if you want the associated Variation Attributes to be automatically deleted after deleting a Variation, here’s a code suggestion on how to do it:
Deleting All Variation Attributes from original Variable Product:
Instead of cloning the object, you can also create a new
WC_Product_Attribute()
instance and assign all the necessary data manually.