为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
呼如林 2023-11-03 15:25:53
我有一个分类属性,我需要更新属性标签。这是我到目前为止所做的 $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();  }如果我读回产品属性,标签不会更新(读取旧标签),我需要更新属性标签并将其保存到数据库中,以便当读回值时,它反映新更新的标签。我缺少什么?
查看完整描述

1 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

要更改/更新产品属性分类数据,您需要使用wc_update_attribute()function,因此在代码中更改产品属性标签名称:

$products  = wc_get_products( array('category' => 't-shirts',  'orderby' => 'name') );


// Loop through queried products

foreach($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,

            ) );

        }

    }

}

经过测试并有效。


查看完整回答
反对 回复 2023-11-03
  • 1 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信