php 在 Woocommerce 中获取自定义产品属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13374883/
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 05:16:33  来源:igfitidea点击:

Get custom product attributes in Woocommerce

phpwordpresswoocommerceproductcustom-taxonomy

提问by Nick

In Woocommerce, I am trying to get product custom attribute values but I fail miserably and I don't get anything.

在 Woocommerce 中,我试图获取产品自定义属性值,但我失败了,我什么也没得到。

So I tried:

所以我试过:

global $woocommerce, $post, $product;
$res = get_post_meta($product->id);
print_r(unserialize($res['_product_attributes'][0]));

And I'm getting this raw data:

我得到了这些原始数据:

[pa_koostis] => Array
        (
            [name] => pa_koostis
            [value] => 
            [position] => 0
            [is_visible] => 1
            [is_variation] => 0
            [is_taxonomy] => 1
        )

I know that there is a value because it is shown in the attribute section, but I just can't find a way to get it displayed with my custom code.

我知道有一个值,因为它显示在属性部分,但我只是找不到一种方法来使用我的自定义代码显示它。

回答by Nick

Edited: The woocommerce_get_product_termsis deprecated since Woocommerce version 3

已编辑自 Woocommerce 版本 3 起woocommerce_get_product_terms已弃用

Go with the following as @datafeedrwrote in his answer:

正如@datafeedr在他的回答中所写的那样

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

or even more compact:

甚至更紧凑:

global $product;
$koostis = $product->get_attribute( 'pa_koostis' );


Original answer:

原答案:

$result = array_shift(woocommerce_get_product_terms($product->id, 'pa_koostis', 'names'));

回答by david_nash

Update for 2018. You can use:

2018 年更新。您可以使用:

global $product;
echo wc_display_product_attributes( $product );

To customise the output, copy plugins/woocommerce/templates/single-product/product-attributes.phpto themes/theme-child/woocommerce/single-product/product-attributes.phpand modify that.

自定义输出,复制plugins/woocommerce/templates/single-product/product-attributes.phpthemes/theme-child/woocommerce/single-product/product-attributes.php和修改。

回答by datafeedr

woocommerce_get_product_terms()is now deprecated.

woocommerce_get_product_terms()现在已弃用。

Use wc_get_product_terms()instead.

使用wc_get_product_terms()来代替。

Example:

例子:

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

回答by Artipixel

Most updated:

最新更新:

$product->get_attribute( 'your_attr' );

You will need to define $productif it's not on the page.

您需要定义$product它是否不在页面上。

回答by terrytsang

You can get the single value for the attribute with below code:

您可以使用以下代码获取属性的单个值:

$pa_koostis_value = get_post_meta($product->id, 'pa_koostis', true);

回答by airdrumz

Try this to get an array of attribute name => attribute value(s):

试试这个来获取属性名称 => 属性值的数组:

global $product;

$formatted_attributes = array();

$attributes = $product->get_attributes();

foreach($attributes as $attr=>$attr_deets){

    $attribute_label = wc_attribute_label($attr);

    if ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) {

        $attribute = isset( $attributes[ $attr ] ) ? $attributes[ $attr ] : $attributes[ 'pa_' . $attr ];

        if ( $attribute['is_taxonomy'] ) {

            $formatted_attributes[$attribute_label] = implode( ', ', wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) ) );

        } else {

            $formatted_attributes[$attribute_label] = $attribute['value'];
        }

    }
}

//print_r($formatted_attributes);

return $formatted_attributes;

It's little inefficient but does the trick.

这有点低效,但可以解决问题。

回答by jere_hr

The answer to "Any idea for getting all attributes at once?" question is just to call function with only product id:

“有没有办法一次性获取所有属性?”的答案。问题只是调用只有产品 id 的函数:

$array=get_post_meta($product->id);

key is optional, see http://codex.wordpress.org/Function_Reference/get_post_meta

键是可选的,见http://codex.wordpress.org/Function_Reference/get_post_meta

回答by Nishad Up

Use below code to get all attributes with details

使用以下代码获取所有属性的详细信息

    global $wpdb;

    $attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name != '' ORDER BY attribute_name ASC;" );
    set_transient( 'wc_attribute_taxonomies', $attribute_taxonomies );

    $attribute_taxonomies = array_filter( $attribute_taxonomies  ) ;

    prin_r($attribute_taxonomies);