SNikolic
SNikolic

Reputation: 71

How to delete a variation from a Woocommerce variable product in php

I have a variable product with 5 variations (based on a single attribute - size).

So:

Product "Shirt" - product_id = 1234

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.

Upvotes: 5

Views: 7777

Answers (2)

Prid
Prid

Reputation: 1624

WooCommerce 3.0+

To Delete a Variation (source code):

$variation_product = wc_get_product( 1239 );

$variation_product->delete( true );
// true = force delete
// false = move to trash

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:

Variable Product attributes (before): XS, S, M, L, XL
Variation attributes: XL
Variable Product attributes (after): XS, S, M, L
// Variable Product
$product = wc_get_product( 1234 );
$product_attributes = $product->get_attributes(); // XS, S, M, L, XL

// XL Variation
$variation = wc_get_product( 1239 );
$variation_attributes = $variation->get_attributes(); // XL

// loop through Variation attributes (here: attribute is Size, values are XL)
foreach( $variation_attributes as $attribute => $value ){

    // Fetch attributes: Size => XS, S, M, L, XL
    $product_attribute_options = $product_attributes[ $attribute ]->get_options();

    // remove "XL" option
    remove_value_from_array( $product_attribute_options, $value );

    // reassign Size WITHOUT "XL" option
    $product_attributes[ $attribute ]->set_options( $product_attribute_options );

    // clone WC_Product_Attribute object to tell WooCommerce it has changed (otherwise save() won't work)
    $product_attributes[ $attribute ] = clone $product_attributes[ $attribute ];
}

// assign the new attributes object to main Variable product
$product->set_attributes( $product_attributes );
$product->save(); // save to DB

$variation->delete(); // finally, delete variation


// credit: https://stackoverflow.com/a/11982622
function remove_value_from_array(& $array, $value){
    foreach (array_keys($array, $value, true) as $key) {
        unset($array[$key]);
    }
}

Instead of cloning the object, you can also create a new WC_Product_Attribute() instance and assign all the necessary data manually.

Upvotes: 3

Benjamin
Benjamin

Reputation: 43

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

Upvotes: 3

Related Questions