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

时间:2022-01-22 08:17:02

I am trying to get products custom attribute values but I am miserably failing to do so.

我试图获得产品自定义属性值,但我很难做到这一点。

I tried to do:

我试着做:

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 shows it in the attribute section but I just cant find a way to get it to show in my custom code.

我知道有一个值,因为它在属性部分显示它,但我无法找到一种方法让它在我的自定义代码中显示。

9 个解决方案

#1


67  

Edited: The woocommerce_get_product_terms is deprecated since Woocommerce version 3

编辑:自Woocommerce第3版以来,不推荐使用woocommerce_get_product_terms

Go with the following as @datafeedr wrote 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'));

#2


13  

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

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

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

#3


13  

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' ) ) );

#4


9  

Most updated:

最新的:

$product->get_attribute( 'your_attr' );

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

如果$ product不在页面上,则需要定义$ product。

#5


9  

Update for 2016. You can use:

2016年更新。您可以使用:

global $product;
echo $product->list_attributes();

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

要自定义输出,请将plugins / woocommerce / templates / single-product / product-attributes.php复制到themes / theme-child / woocommerce / single-product / product-attributes.php并进行修改。

#6


8  

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.

它效率不高但可以解决问题。

#7


2  

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

key是可选的,请参阅http://codex.wordpress.org/Function_Reference/get_post_meta

#8


1  

You will get attributes as array in "$formatted_attributes"

您将在“$ formatted_attributes”中获取数组属性

          $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] = wc_get_product_terms( $product->id, $attribute['name']);

                   } else {

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

               }
           }


           print_r($formatted_attributes);

#9


0  

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);

#1


67  

Edited: The woocommerce_get_product_terms is deprecated since Woocommerce version 3

编辑:自Woocommerce第3版以来,不推荐使用woocommerce_get_product_terms

Go with the following as @datafeedr wrote 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'));

#2


13  

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

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

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

#3


13  

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' ) ) );

#4


9  

Most updated:

最新的:

$product->get_attribute( 'your_attr' );

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

如果$ product不在页面上,则需要定义$ product。

#5


9  

Update for 2016. You can use:

2016年更新。您可以使用:

global $product;
echo $product->list_attributes();

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

要自定义输出,请将plugins / woocommerce / templates / single-product / product-attributes.php复制到themes / theme-child / woocommerce / single-product / product-attributes.php并进行修改。

#6


8  

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.

它效率不高但可以解决问题。

#7


2  

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

key是可选的,请参阅http://codex.wordpress.org/Function_Reference/get_post_meta

#8


1  

You will get attributes as array in "$formatted_attributes"

您将在“$ formatted_attributes”中获取数组属性

          $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] = wc_get_product_terms( $product->id, $attribute['name']);

                   } else {

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

               }
           }


           print_r($formatted_attributes);

#9


0  

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);