在WooCommerce 3.0+中将属性添加到简短描述 [英] Add Attributes to Short Description in WooCommerce 3.0+

查看:156
本文介绍了在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

还有一个: Woocommerce-显示单个产品属性( s)在前端使用短代码

我无法使其工作.我认为应该是因为WooCommerce已更新至3.0+版

And I wasn't able to make it work. I think it should be because WooCommerce got updated to version 3.0+

有人知道做到这一点的方法吗?

Does anyone know a way to make it?

谢谢

推荐答案

更新3 (简单产品的自动化,WC兼容性)

Update 3 ( Automation for simple products, WC compatibility )

// 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>';
}


更新(仅适用于WooCommerce 3.0+版本).


Update For WooCommerce version 3.0+ only.

// 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 ' / ';
            }
        }
    }
}

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

它现在应该可以在WC 3.0+中使用

It should work now in WC 3.0+

与该答案代码有关:显示归档类别页面上的特定产品属性值

这篇关于在WooCommerce 3.0+中将属性添加到简短描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆