Php 显示属性值说明而不是值名称

Php 显示属性值说明而不是值名称,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我有这个代码来显示产品属性 add_action('woocommerce_single_product_summary', 'attributes', 30 ); function attributes() { global $product; $attributes_names = array('Size', 'Color', 'Material', 'Pieces'); $attributes_data = array(); foreach ( $attributes_names a

我有这个代码来显示产品属性

add_action('woocommerce_single_product_summary', 'attributes', 30 );

function attributes() {
global $product;
$attributes_names = array('Size', 'Color', 'Material', 'Pieces');
$attributes_data  = array();

foreach ( $attributes_names as $attribute_name ) {
    if ( $value = $product->get_attribute($attribute_name) ) {
        $attributes_data[] = $attribute_name . ': ' . $value;
    }
    
}

if ( ! empty($attributes_data) ) {
    echo '<div><p>' . implode( '</p>', $attributes_data ) . '</div>';
}   
}

如何做到这一点?

您可以使用
wc\u get\u product\u terms
检索产品的术语(包括其属性)。函数的参数是产品ID、分类法和查询参数

例如,您可以这样做:

function attributes() {
  global $product;

  $attributes_names = array( 'Size', 'Color', 'Material', 'Pieces' );
  $attributes_data = array();
  foreach ( $product->get_attributes() as $attribute => $value ) {
      $attribute_name = wc_get_attribute( $value->get_id() )->name;
      if ( ! in_array( $attribute_name, $attributes_names, true ) ) {
        continue;
      }
      $values = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
      if ( $values ) {
          $attribute_descriptions = array();
          foreach ( $values as $term ) {
              $attribute_descriptions[] = term_description( $term->term_id, $term->taxonomy );
          }
          $attributes_data[] = $attribute_name . ': ' . implode( ',', $attribute_descriptions );
      }
  }
  if ( ! empty( $attributes_data ) ) {
      echo '<div><p>' . implode( '</p>', $attributes_data ) . '</div>';
  }
}
函数属性(){
全球$产品;
$attributes_names=数组('Size'、'Color'、'Material'、'parties');
$attributes_data=array();
foreach($product->get_attributes()作为$attribute=>$value){
$attribute\u name=wc\u get\u attribute($value->get\u id())->name;
if(!in_数组($attribute_name,$attributes_name,true)){
继续;
}
$values=wc_get_product_terms($product->get_id(),$attribute,数组('fields'=>'all'));
如果($value){
$attribute_descriptions=array();
foreach(价值为$term){
$attribute\u descriptions[]=术语描述($term->term\u id,$term->taxonomy);
}
$attributes\u data[]=$attribute\u name.':'.内爆(',',$attribute\u descriptions);
}
}
如果(!空($attributes\u data)){
回显“”。内爆(“

”,$attributes\u data)。“”; } }

要注意的是,属性描述包括任何HTML标记、换行符等。这就是为什么在上面的示例中我去掉了这些标记,但当然可能没有必要。尽管如此,这显示了所有属性-我只需要显示代码中提到的属性,只有当它们存在/被设置为产品时。如何在描述中保留html?要仅显示属性的子集,只需检查名称即可。另外,
wc\u get\u product\u terms
将仅返回产品上设置的属性。至于保留HTML标记,您只需消除对
wp\u filter\u nohtml\u kses
str\u replace
的调用即可。我编辑了我的答案以反映这些变化。
function attributes() {
  global $product;

  $attributes_names = array( 'Size', 'Color', 'Material', 'Pieces' );
  $attributes_data = array();
  foreach ( $product->get_attributes() as $attribute => $value ) {
      $attribute_name = wc_get_attribute( $value->get_id() )->name;
      if ( ! in_array( $attribute_name, $attributes_names, true ) ) {
        continue;
      }
      $values = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
      if ( $values ) {
          $attribute_descriptions = array();
          foreach ( $values as $term ) {
              $attribute_descriptions[] = term_description( $term->term_id, $term->taxonomy );
          }
          $attributes_data[] = $attribute_name . ': ' . implode( ',', $attribute_descriptions );
      }
  }
  if ( ! empty( $attributes_data ) ) {
      echo '<div><p>' . implode( '</p>', $attributes_data ) . '</div>';
  }
}