2
votes

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.

I need to remove this XL as well

Does anybody have a php code snipped that would fully remove a product variation including this attribute entry.

1
wc_delete_post() does not exist as far as I can find on the mighty internet. wp_delete_post() does. - Mike

1 Answers

3
votes

You can get rid of the attribute in the product with:

wp_remove_object_terms( product_id, term_slug_or_id, taxonomy_name );

More here: https://codex.wordpress.org/Function_Reference/wp_remove_object_terms