1
votes

I'm programmatically creating attribute variations (variable product). Everything I've seen does this easily, but the value simply does not show up. Here's my code:

$thedata = Array(
     'pa_performance_dates' =>  Array( 
          'name'        => 'pa_performance_dates', 
           'value'      =>  $arrayOfTermIDs,
           'position'   => 1,
           'is_visible' => '1',
           'is_variation' => '1',
           'is_taxonomy' => '1'
      )
);
update_post_meta( $product_id, '_product_attributes', $thedata );

Attribute: "Performance Dates" WORKING
Attribute Values (terms under attribute): "1571986800" WORKING
Attribute Values being added under the Product ID: NOT WORKING

attribute values still empty

I have tried everything: setting the VALUE above to an array of the terms slugs, an array of the IDs, a simple string of an ID or slug/value. Nothing works. I've also followed these stackoverflow questions:
woocommerce: add value to a product attribute
Creating WooCommerce product variation adds an empty attribute value
Add Product Attributes with values to a product in Woocommerce

I've also tested the product_id variable and that is correctly showing the ID of the post.

What am I doing wrong?!?!?!?

1

1 Answers

0
votes

To add value to an attribute of a product use wp_set_object_terms

For example: $arrayOfTermIDs = array( 'Test1', 'Test2', 'Test3' );

foreach ($arrayOfTermIDs as $arrayOfTermID) {
    $term_taxonomy_ids = wp_set_object_terms( $product_id, $arrayOfTermID, 'pa_performance_dates', true );          
}

To append the value set the last argument as true or set it to false to overwrite.