亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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 關注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號