如何在 WooCommerce 中更新产品属性分类标签名称

我有一个分类属性,我需要更新属性标签。这是我到目前为止所做的


 $args = array(

      'category' => array('chinese'),

      'orderby' => 'name',

  );

  $products = wc_get_products($args);

  foreach($products as $product)

  {

     $attribute = $product->get_attributes();

  

             foreach($attribute as $attributeItem)

      {

  

          if($attributeItem->is_taxonomy())

          {

             $attributeItem->get_taxonomy_object()->attribute_label = "new-label"; // set new label

              

          }

      } 

      $product->set_attributes($attribute);

      $product-save();

  }

如果我读回产品属性,标签不会更新(读取旧标签),我需要更新属性标签并将其保存到数据库中,以便当读回值时,它反映新更新的标签。


我缺少什么?


呼如林
浏览 72回答 1
1回答

子衿沉夜

要更改/更新产品属性分类数据,您需要使用wc_update_attribute()function,因此在代码中更改产品属性标签名称:$products  = wc_get_products( array('category' => 't-shirts',  'orderby' => 'name') );// Loop through queried productsforeach($products as $product) {    // Loop through product attributes    foreach( $product->get_attributes() as $attribute ) {        if( $attribute->is_taxonomy() ) {            $attribute_id   = $attribute->get_id(); // Get attribute Id                        $attribute_data = wc_get_attribute( $attribute_id ); // Get attribute data from the attribute Id                        // Update the product attribute with a new taxonomy label name            wc_update_attribute( $attribute_id, array(                'name'         => 'New label', // <== == == Here set the taxonomy label name                'slug'         => $attribute_data->slug,                'type'         => $attribute_data->type,                'order_by'     => $attribute_data->order_by,                'has_archives' => $attribute_data->has_archives,            ) );        }    }}经过测试并有效。
打开App,查看更多内容
随时随地看视频慕课网APP