【问题标题】:Echo value of product custom attribute - WooCommerce产品自定义属性的回声值 - WooCommerce
【发布时间】:2016-07-13 16:59:12
【问题描述】:

我需要有关 WordPress Woocommerce 的帮助。

如何回显产品自定义属性的值?

问题:属性值全部打印在一行中。 我只需要在每一行打印出 1 个值

这是我的代码。

global $product;

if ( ! $product->is_purchasable() ) {
  return;
}
$custom_attr_values = array_shift( wc_get_product_terms( $product->id, 'first-attribute-name', array( 'fields' => 'names' ) ) );
?>

<div class="selection">
  <font color="white">First Attribute Name</font>
  <select>
    <option value=""><?php
      $subheadingvalues = get_the_terms( $product->id, 'pa_first-attribute-name');
 foreach ( $subheadingvalues as $subheadingvalue ) {
        echo $subheadingvalue->name;
      }
      ?></option>
    <option value=""><?php
      $subheadingvalues = get_the_terms( $product->id, 'pa_first-attribute-name');

      foreach ( $subheadingvalues as $subheadingvalue ) {
        echo $subheadingvalue->name;
      }
      ?></option>
    <option value=""><?php
      $subheadingvalues = get_the_terms( $product->id, 'pa_first-attribute-name');

      foreach ( $subheadingvalues as $subheadingvalue ) {
        echo $subheadingvalue->name;
      }
      ?></option>
    <option value=""><?php
      $subheadingvalues = get_the_terms( $product->id, 'pa_first-attribute-name');

      foreach ( $subheadingvalues as $subheadingvalue ) {
        echo $subheadingvalue->name;
      }
      ?></option>
  </select>

【问题讨论】:

    标签: php woocommerce attributes echo


    【解决方案1】:

    您可以使用 get_terms() 函数获取下拉列表值http://codex.wordpress.org/Function_Reference/get_terms

    如果您传入自定义属性名称,您将返回该分类中的术语列表。

    global $product;
    $custom_attr_values = array_shift( wc_get_product_terms( $product->id, 'custom_attr_name', array( 'fields' => 'names' ) ) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-08
      • 2016-12-20
      • 2016-11-24
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      相关资源
      最近更新 更多