简体   繁体   English

在 WooCommerce 3.0+ 中为简短描述添加属性

[英]Add Attributes to Short Description in WooCommerce 3.0+

I would like to insert the attributes from all products to their short description, so the client can open a quickview and check this attributes.我想将所有产品的属性插入到它们的简短描述中,以便客户可以打开快速查看并检查此属性。

I already tried this answer: Display specific product attribute values on archives category pages我已经尝试过这个答案: 在档案类别页面上显示特定的产品属性值

Also this one: Woocommerce - Display single product attribute(s) with shortcodes in Frontend还有这个: Woocommerce - 在前端显示带有短代码的单个产品属性

And I wasn't able to make it work.我无法让它发挥作用。 I think it should be because WooCommerce got updated to version 3.0+我认为应该是因为 WooCommerce 已更新到 3.0+ 版

Does anyone know a way to make it?有谁知道制作方法?

Thanks谢谢

to build off of LoicTheAztec's answer:以 LoicTheAztec 的回答为基础:

His code only works if you have predefined the attributes in the WP backend under Products -> Attributes.只有在 WP 后端的 Products -> Attributes 下预定义了属性,他的代码才有效。 If you work with individual (custom) product attributes that you set up on the product page, wc_get_product_terms() won't return anything.如果您使用在产品页面上设置的单个(自定义)产品属性,则 wc_get_product_terms() 不会返回任何内容。 You can recognize predefined attributes by the "pa_" prefix, they are stored in the "woocommerce_attribute_taxonomies" table.您可以通过“pa_”前缀识别预定义的属性,它们存储在“woocommerce_attribute_taxonomies”表中。

In order to show these individual attributes in the same way as LoicTheAztec suggested, use this code:为了以与 LoicTheAztec 建议的相同方式显示这些单独的属性,请使用以下代码:

add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20);
function custom_attributes_display()
{
    // Just for product category archive pages
    if(is_product_category())
    {
        global $product;

        // get all product attributes
        $attributes = $product->get_attributes();
        // the array of attributes you want to display (shown in same order)
        $show_attributes = array('My Attribute A', 'Another Attribute B');
        foreach($show_attributes as $key => $show_attribute)
        {
            foreach($attributes as $attribute)
            {
                // check if current attribute is among the ones to be shown
                if ($attribute->get_name() == $show_attribute)
                {
                    echo $attribute->get_options()[0];
                    // seperate attributes by "/"
                    if (count($show_attributes) > 1)
                        echo '/';
                    unset($show_attributes[$key]);
                    break;
                }
            }
        }
    }
}

Update 3 ( Automation for simple products, WC compatibility )更新 3 (简单产品的自动化,WC 兼容性)

// Compatibility for WC 3+ and automation enhancements
add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20 );
function custom_attributes_display(){
    global $product;

    // Just for simple products
    if( ! $product->is_type( 'simple' ) ) return;

    $loop_count = 0;

    echo '<div>';

    // Get the attributes taxonomy slugs (Updated and dynamic now)
    $attributes_taxonomy = $product->get_attributes();
    // OR set an indexed array of taxonomy slug (key) and name (value) to chose which ones, like:
    // $attributes_taxonomy = array('pa_nopeus' => 'Nopeus', 'pa_liito' => 'Liito, 'pa_vakaus' => 'Vaukaus' );

    foreach( $attributes_taxonomy as $taxonomy => $attribute ) {

        // Getting the term names of an attribute (set in a coma separated string if many values)
        $attribute_terms = wp_get_post_terms( get_the_id(), $taxonomy, array( 'fields' => 'names' ) );
        $terms_string = implode( ',', $attribute_terms );

        // Displays only if attribute exist for the product
        if( count( $attribute_terms ) > 0 ){ // Updated
            echo $terms_string;

            // Separating each number by a " | " (Updated and dynamic now)
            $attr_count = count( $attributes_taxonomy );
            $loop_count++;
            if( $loop_count < $attr_count && $attr_count > 1 ) echo ' | ';
        }
    }

    echo '</div>';
}

Update For WooCommerce version 3.0+ only.更新WooCommerce 3.0+ 版。

// For WooCommerce Version 3.0+ (only)
add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20 );
function custom_attributes_display(){

    // Just for product category archives pages
    if(is_product_category()){
        global $product;

        // the array of attributes names
        $attribute_names = array('pa_nopeus', 'pa_liito', 'pa_vakaus', 'pa_feidi');
        foreach( $attribute_names as $key => $attribute_name ) {

            // For WooCommerce version 3.0+
            $product_id = $product->get_id(); // WC 3.0+

            // Getting the value of an attribute (OK for WC 3.0+)
            $wc_term = wc_get_product_terms( $product_id, $attribute_name);
            $attribute_value = array_shift($wc_term);

            // Displays only if attribute exist for the product
            if(!empty($attribute_value) || '0' == $attribute_value ){ // Updated
                echo $attribute_value;

                // Separating each number by a " / "
                if($key < 3) echo ' / ';
            }
        }
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

It should work now in WC 3.0+它现在应该可以在 WC 3.0+ 中使用


Related to this Answer code: Display specific product attribute values on archives category pages与此答案代码相关: 在档案类别页面上显示特定的产品属性值

I would like to insert the attributes from all products to their short description, so the client can open a quickview and check this attributes.我想将所有产品的属性插入到它们的简短描述中,以便客户端可以打开快速查看并检查此属性。

I already tried this answer: Display specific product attribute values on archives category pages我已经尝试过以下答案: 在存档类别页面上显示特定的产品属性值

Also this one: Woocommerce - Display single product attribute(s) with shortcodes in Frontend还有一个: Woocommerce-在前端中显示带有短代码的单个产品属性

And I wasn't able to make it work.而且我无法使其工作。 I think it should be because WooCommerce got updated to version 3.0+我认为应该是因为WooCommerce已更新至版本3.0+

Does anyone know a way to make it?有谁知道做到这一点的方法吗?

Thanks谢谢

I was trying to do pretty much the same and found a one-liner solution that might help people dropping by here as I did.我试图做几乎相同的事情,并找到了一种单线解决方案,可以帮助人们像我一样来这里。 The following code works for me.以下代码对我有用。

WooCommerce Version: 5+ WooCommerce 版本:5+

add_action( 'woocommerce_single_product_summary', 'add_atts_to_summary' );

function add_atts_to_summary() {
    global $product;
    wc_display_product_attributes( $product );
}

i've got a question related on this, is there a posibility (looks like a shortcode how we implement atribute values in the short & long description.我有一个与此相关的问题,是否有可能(看起来像一个简码,我们如何在短描述和长描述中实现属性值。

like we set 1 text for every product in a category and that the values change based on the atributes that are set.就像我们为类别中的每个产品设置 1 个文本,并且值根据设置的属性而变化。

as example for this link =>https://www.amazon.com/Roku-Streaming-Device-Vision-Controls/dp/B09BKCDXZC/ref=lp_16225007011_1_2作为此链接的示例 => https://www.amazon.com/Roku-Streaming-Device-Vision-Controls/dp/B09BKCDXZC/ref=lp_16225007011_1_2

short discription: this 4K streaming stick from {brand}, the stick can be connected true {Connectivity Technology}.简短说明:这款来自{brand} 的4K 流媒体棒,该棒可以连接真正的{Connectivity Technology}。 the stick can be controlled true {Controller Type}摇杆可以控制真正的{控制器类型}

does someone know how to get this ?有人知道如何得到这个吗?

thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM