Tania
Tania

Reputation: 29

How to programmatically create a Variable Product using existing Product Attributes

Any advice would be most appreciated. I have the following code that creates the Variable Product but the Attributes (and therefore the Variations) are not being attached.

$new_variable_product = new WC_Product_Variable();
 $new_variable_product->set_name("Book Your Event");
 $new_variable_product->set_slug($permalink);
 $new_variable_product->set_short_description('Hello world');
 $new_variable_product->set_status('publish');
 $new_variable_product->save();
 //*************************//
    $attribute = new WC_Product_Attribute();
    
    // get a product attribute ID by name.
    $attribute_id = wc_attribute_taxonomy_id_by_name( 'Event Purpose' );
    $attribute->set_id( $attribute_id );
    $attribute->set_name( 'pa_type-of-event' ); // -> removed 'pa_' prefix
    
    //Set terms slugs
    $attribute->set_options( array(
        'Hens Party', 'Social Party', 'Team Building'
    ) );
    $attribute->set_position( 0 );
    $attribute->set_visible( 1 );//If enabled
    $attribute->set_variation( 1 );//If we are going to use attribute in order to generate variations
    
    //Create the attribute2 object
    $attribute2 = new WC_Product_Attribute();
    
    // get a product attribute ID by name.
    $attribute_id = wc_attribute_taxonomy_id_by_name( 'Estimated Guest No' );
    $attribute2->set_id( $attribute_id );
    $attribute2->set_name( 'pa_participants' ); // -> removed 'pa_' prefix
    
    //Set terms slugs
    $attribute2->set_options( array(
        '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'
    ) );
    $attribute2->set_position( 0 );
    $attribute2->set_visible( 1 );//If enabled
    $attribute2->set_variation( 1 );//If we are going to use attribute in order to generate variations
    
    $new_variable_product->set_attributes(array($attribute, $attribute2));
    
    //Save main product to get its id
    $thisid = $new_variable_product->save();
    
    $variation = new WC_Product_Variation();
    $variation->set_regular_price($price);
    $variation->set_parent_id($thisid);
    
    //Set attributes requires a key/value containing
    // tax and term slug
    $variation->set_attributes(array(
        'pa_type-of-event' => '', 
        'pa_participants' => ''
    ));
    $variation->save(); 

There are existing Attributes with the name, as per below. enter image description here

I have noticed that this line

$attribute_id = wc_attribute_taxonomy_id_by_name( 'Event Purpose' );

is returning $attribute_id as 0. Why?

Upvotes: 0

Views: 406

Answers (1)

Prid
Prid

Reputation: 1624

I have noticed that this line

$attribute_id = wc_attribute_taxonomy_id_by_name( 'Event Purpose' );

is returning $attribute_id as 0. Why?

Let me answer that part of your question, and then hopefully you can figure out the rest yourself.

Explanation

wc_attribute_taxonomy_id_by_name internally works by, according to the source code:

  1. Formatting the provided attribute name to a "slug"-friendly format (Event Purpose -> event-purpose)
  2. Looking up all the defined taxonomies by their slug name to see if your inputted one exists there (checking if event-purpose slug exists)
    • if it DOES, return its ID
    • if it DOESN'T, return 0

Looking at the screenshot you've posted, it's obvious that there's a mismatch between your custom-defined slug, type-of-events, and the one the functions uses, event-purpose.

The function's name does imply that it's looking up by its name, but that's not how it works internally, unfortunately.

Fix

  • Change Event Purpose's slug to event-purpose

-- OR --

  • Change the Attribute name to Type of Event (which will translate to type-of-event internally)
$attribute_id = wc_attribute_taxonomy_id_by_name( 'Type of Event' );

Upvotes: 1

Related Questions