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

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

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

问题

我有一个属性:

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

我尝试了这段代码:

  1. $attribute_slug = 'pa_box-size';
  2. // 根据属性标识获取术语对象
  3. $term = get_term_by('slug', $attribute_slug, 'pa');
  4. if ($term) {
  5. $attribute_label = $term->name; // 属性标签
  6. $attribute_id = $term->term_id; // 属性ID
  7. // 输出属性标识和ID
  8. echo '属性标识:' . $attribute_slug . '<br>';
  9. echo '属性标签:' . $attribute_label . '<br>';
  10. echo '属性ID:' . $attribute_id;
  11. } else {
  12. echo '未找到属性:' . $attribute_slug;
  13. }

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

英文:

I have an attribute:<br>

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

I tried this code:

  1. $attribute_slug = &#39;pa_box-size&#39;;
  2. // Get the term object based on the attribute slug
  3. $term = get_term_by(&#39;slug&#39;, $attribute_slug, &#39;pa&#39;);
  4. if ($term) {
  5. $attribute_label = $term-&gt;name; // Attribute label
  6. $attribute_id = $term-&gt;term_id; // Attribute ID
  7. // Output the attribute label and ID
  8. echo &#39;Attribute Slug: &#39; . $attribute_slug . &#39;&lt;br&gt;&#39;;
  9. echo &#39;Attribute Label: &#39; . $attribute_label . &#39;&lt;br&gt;&#39;;
  10. echo &#39;Attribute ID: &#39; . $attribute_id;
  11. } else {
  12. echo &#39;Attribute not found: &#39; . $attribute_slug;
  13. }

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

答案1

得分: 0

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

  1. $output = '<ul style="list-style:none;">';
  2. // 遍历所有的Woocommerce产品属性
  3. foreach (wc_get_attribute_taxonomies() as $attribute) {
  4. $attribute_label = $attribute->attribute_label; // 产品属性名称
  5. $attribute_slug = $attribute->attribute_name; // 产品属性别名
  6. $attribute_id = $attribute->attribute_id; // 属性ID
  7. $output .= '<li class="' . $attribute_slug . '" data-id="' . $attribute_id . '">'
  8. . $attribute_label . '</li>';
  9. }
  10. // 输出
  11. echo $output.'</ul>';

来源:wordpress.stackexchange.com

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

英文:

I solved my problem with this:

  1. $output = &#39;&lt;ul style=&quot;list-style:none;&quot;&gt;&#39;;
  2. // Loop through all Woocommerce product attributes
  3. foreach (wc_get_attribute_taxonomies() as $attribute) {
  4. $attribute_label = $attribute-&gt;attribute_label; // Product attribute name
  5. $attribute_slug = $attribute-&gt;attribute_name; // Product attribute slug
  6. $attribute_id = $attribute-&gt;attribute_id; // Attribute ID
  7. $output.= &#39;&lt;li class=&quot;&#39;.$attribute_slug.&#39;&quot; data-id=&quot;&#39;.$attribute_id.&#39;&quot;&gt;&#39;
  8. .$attribute_label.&#39;&lt;/li&gt;&#39;;
  9. }
  10. // Output
  11. 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

你可以尝试这段代码。

  1. $attribute_slug = 'color'; // 用你的属性别名替换这里
  2. $product_attributes = wc_get_attribute_taxonomies();
  3. foreach ($product_attributes as $product_attribute) {
  4. if ($product_attribute->attribute_name === $attribute_slug) {
  5. $attribute_label = $product_attribute->attribute_label;
  6. $attribute_id = $product_attribute->attribute_id;
  7. echo '属性标签: ' . $attribute_label . '<br>';
  8. echo '属性ID: ' . $attribute_id;
  9. break;
  10. }
  11. }

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

英文:
  1. You can try this code.
  2. $attribute_slug = &#39;color&#39;; // Replace with your attribute slug
  3. $product_attributes = wc_get_attribute_taxonomies();
  4. foreach ($product_attributes as $product_attribute) {
  5. if ($product_attribute-&gt;attribute_name === $attribute_slug) {
  6. $attribute_label = $product_attribute-&gt;attribute_label;
  7. $attribute_id = $product_attribute-&gt;attribute_id;
  8. echo &#39;Attribute Label: &#39; . $attribute_label . &#39;&lt;br&gt;&#39;;
  9. echo &#39;Attribute ID: &#39; . $attribute_id;
  10. break;
  11. }
  12. }

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:

确定