获取WordPress中WooCommerce属性标签和ID的方法如何从slug名称中获取?

huangapple go评论128阅读模式
英文:

How to get WooCommerce attribute label and id from slug name in WordPress?

问题

我有一个属性:

  • 名称(label):盒子尺寸
  • 标识:box-size

我尝试了这段代码:

$attribute_slug = 'pa_box-size';

// 根据属性标识获取术语对象
$term = get_term_by('slug', $attribute_slug, 'pa');

if ($term) {
   $attribute_label = $term->name; // 属性标签
   $attribute_id = $term->term_id; // 属性ID

   // 输出属性标识和ID
   echo '属性标识:' . $attribute_slug . '<br>';
   echo '属性标签:' . $attribute_label . '<br>';
   echo '属性ID:' . $attribute_id;
} else {
   echo '未找到属性:' . $attribute_slug;
}

但始终得到 "未找到属性: pa_box-size"。

英文:

I have an attribute:<br>

  • Name(label): Box Size
  • Slag: box-size

I tried this code:

$attribute_slug = &#39;pa_box-size&#39;; 

// Get the term object based on the attribute slug
$term = get_term_by(&#39;slug&#39;, $attribute_slug, &#39;pa&#39;);

if ($term) {
   $attribute_label = $term-&gt;name; // Attribute label
   $attribute_id = $term-&gt;term_id; // Attribute ID

   // Output the attribute label and ID
   echo &#39;Attribute Slug: &#39; . $attribute_slug . &#39;&lt;br&gt;&#39;;
   echo &#39;Attribute Label: &#39; . $attribute_label . &#39;&lt;br&gt;&#39;;
   echo &#39;Attribute ID: &#39; . $attribute_id;
} else {
   echo &#39;Attribute not found: &#39; . $attribute_slug;
}

But got always "Attribute not found: pa_box-size".

答案1

得分: 0

我用以下方式解决了我的问题:

$output = '<ul style="list-style:none;">';

// 遍历所有的Woocommerce产品属性
foreach (wc_get_attribute_taxonomies() as $attribute) {
  $attribute_label = $attribute->attribute_label;  // 产品属性名称
  $attribute_slug = $attribute->attribute_name;    // 产品属性别名
  $attribute_id = $attribute->attribute_id;        // 属性ID

  $output .= '<li class="' . $attribute_slug . '" data-id="' . $attribute_id . '">'
             . $attribute_label . '</li>';
}

// 输出
echo $output.'</ul>';

来源:wordpress.stackexchange.com

所以,我只需要一个IF语句来检查特定的属性。

英文:

I solved my problem with this:

$output = &#39;&lt;ul style=&quot;list-style:none;&quot;&gt;&#39;;

// Loop through all Woocommerce product attributes
foreach (wc_get_attribute_taxonomies() as $attribute) {
  $attribute_label = $attribute-&gt;attribute_label;  // Product attribute name
  $attribute_slug = $attribute-&gt;attribute_name;    // Product attribute slug
  $attribute_id = $attribute-&gt;attribute_id;        // Attribute ID

  $output.= &#39;&lt;li class=&quot;&#39;.$attribute_slug.&#39;&quot; data-id=&quot;&#39;.$attribute_id.&#39;&quot;&gt;&#39;
                .$attribute_label.&#39;&lt;/li&gt;&#39;;
}
// Output
echo $output.&#39;&lt;/ul&gt;&#39;;

Source: wordpress.stackexchange.com

So I will just need an IF statement to check a specific attribute.

答案2

得分: -1

你可以尝试这段代码。

$attribute_slug = 'color'; // 用你的属性别名替换这里

$product_attributes = wc_get_attribute_taxonomies();

foreach ($product_attributes as $product_attribute) {
    if ($product_attribute->attribute_name === $attribute_slug) {
        $attribute_label = $product_attribute->attribute_label;
        $attribute_id = $product_attribute->attribute_id;

        echo '属性标签: ' . $attribute_label . '<br>';
        echo '属性ID: ' . $attribute_id;

        break;
    }
}

请注意,我已经将代码中的HTML实体字符(如&gt;&lt;)还原为正常的PHP代码,以便更好地理解。

英文:
You can try this code.

$attribute_slug = &#39;color&#39;; // Replace with your attribute slug

$product_attributes = wc_get_attribute_taxonomies();

foreach ($product_attributes as $product_attribute) {
    if ($product_attribute-&gt;attribute_name === $attribute_slug) {
        $attribute_label = $product_attribute-&gt;attribute_label;
        $attribute_id = $product_attribute-&gt;attribute_id;

        echo &#39;Attribute Label: &#39; . $attribute_label . &#39;&lt;br&gt;&#39;;
        echo &#39;Attribute ID: &#39; . $attribute_id;

        break;
    }
}

huangapple
  • 本文由 发表于 2023年5月25日 10:11:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定