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

问题描述 投票:0回答:3

我想将所有产品的属性插入到简短描述中,以便客户端可以打开快速查看并检查此属性。

我已经尝试过这个答案:Display specific product attribute values on archives category pages

还有这个:Woocommerce - Display single product attribute(s) with shortcodes in Frontend

我无法使其发挥作用。我认为应该是因为WooCommerce已经更新到3.0+版本

有没有人知道如何制作它?

谢谢

php wordpress woocommerce attributes product
3个回答
1
投票

更新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>';
}

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

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

它现在应该在WC 3.0+中运行


与此答案代码相关:Display specific product attribute values on archives category pages


1
投票

建立LoicTheAztec的答案:

只有在产品 - >属性下的WP后端预定义了属性时,他的代码才有效。如果您使用在产品页面上设置的单个(自定义)产品属性,则wc_get_product_terms()将不返回任何内容。您可以通过“pa_”前缀识别预定义属性,它们存储在“woocommerce_attribute_taxonomies”表中。

为了以与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;
                }
            }
        }
    }
}

1
投票

我如何用这段代码显示属性标签?

// WC 3+的兼容性和自动化增强功能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>';

}

© www.soinside.com 2019 - 2024. All rights reserved.