Reputation: 29
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.
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
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.
wc_attribute_taxonomy_id_by_name
internally works by, according to the source code:
Event Purpose -> event-purpose
)event-purpose
slug exists)
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.
event-purpose
-- OR --
Type of Event
(which will translate to type-of-event
internally)$attribute_id = wc_attribute_taxonomy_id_by_name( 'Type of Event' );
Upvotes: 1