php woocommerce 获取属性值列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20500286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:15:00  来源:igfitidea点击:

woocommerce get list of attribute values

phpwordpresswordpress-pluginwoocommerce

提问by clueless

I'm using woocommerce on wordpress to create a simple shop site and I've added a couple attributes to a product. These are namely, sizeand color. Under size I have a variety of values including Small, Medium and Large. Same with color ie. Red, Blue, Green.

我在 wordpress 上使用 woocommerce 创建一个简单的商店网站,我已经为产品添加了几个属性。这些是,sizecolor。在 size 下,我有多种值,包括 Small、Medium 和 Large。与颜色相同,即。红、蓝、绿。

What I want to do is show these values in a dropdown. Basically just list them out so I can use the values as filters for the shop catalog page.

我想要做的是在下拉列表中显示这些值。基本上只需列出它们,以便我可以将这些值用作商店目录页面的过滤器。

Any help would be great.

任何帮助都会很棒。

EDIT: I've delved into the woocommerce code and api docs and only found this code to pull the attributes.

编辑:我已经深入研究了 woocommerce 代码和 api 文档,并且只找到了这段代码来提取属性。

global $woocommerce;

$attr_tax = $woocommerce->get_attribute_taxonomy_names();

foreach( $attr_tax as $tax ) {
     echo $woocommerce->attribute_taxonomy_name( $tax->attribute_name );
}

What this snippet gives me are the taxonomy slugs only, ie. pa_size and pa_color. I'm very new to woocommerce, but a search in there api docs reveals nothing about how to pull the values of these attributes.

这个片段给我的只是分类slugs,即。pa_size 和 pa_color。我对 woocommerce 很陌生,但是在那里的 api docs 中的搜索没有揭示如何提取这些属性的值。

回答by Steven Jones

You can use get_terms()http://codex.wordpress.org/Function_Reference/get_terms

您可以使用get_terms()http://codex.wordpress.org/Function_Reference/get_terms

If you pass in pa_size or pa_color you will get back a list of terms in that taxonomy.

如果您传入 pa_size 或 pa_color,您将返回该分类法中的术语列表。

回答by runningonplants

Hoping this is helpful to someone:

希望这对某人有帮助:

global $product; 

// Get product attributes
$attributes = $product->get_attributes();

if ( ! $attributes ) {
    echo "No attributes";
}

foreach ( $attributes as $attribute ) {

        echo $attribute['name'] . ": ";
        $product_attributes = array();
        $product_attributes = explode('|',$attribute['value']);

        $attributes_dropdown = '<select>';

        foreach ( $product_attributes as $pa ) {
            $attributes_dropdown .= '<option value="' . $pa . '">' . $pa . '</option>';
        }

        $attributes_dropdown .= '</select>';

        echo $attributes_dropdown;
}

回答by user5029040

This post was written some time ago so I don't know if Woocommerce had this method in its previous incarnations.
For anyone else looking to do this, this line is all you need.

这篇文章是前一段时间写的,所以我不知道 Woocommerce 在以前的化身中是否有这种方法。
对于其他想要这样做的人,这条线就是你所需要的。

$product->list_attributes();

This allows you to customize the order and toggle whether or not you want to display the variation in the backend,

这允许您自定义顺序并切换是否要在后端显示变化,

回答by Arif

In addition to @user5029040 answer, which outputs html, if you want to get an array you can use the following function.

除了输出 html 的@user5029040 答案之外,如果您想获取一个数组,您可以使用以下函数。

$product->get_variation_attributes();