简体   繁体   中英

woocommerce: product attribute will not add value

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

属性值仍然为空

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?????!?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM