在归档类别页面上显示特定的产品属性值

问题描述 投票:2回答:1

我想在WooCommerce的类别页面上显示特定的产品属性。属性将在产品标题之后,简短描述之前显示。

属性是pa_nopeuspa_liitopa_vakauspa_feidi。它们只是数值。我只是想显示值而不是名字,非常像这样:

Product Name
4 / 4 / 1 / 2
Short description

如果产品中不存在这些值,则根本不会显示该行。

我想将此添加到(模板)代码而不是使用插件。我相信它会被添加到content-product.php.

#content-product.php start.... 

do_action( 'woocommerce_shop_loop_item_title' );

VALUES HERE SOMEHOW?


/**
 * woocommerce_after_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_rating - 5
 * @hooked woocommerce_template_loop_price - 10
 */
do_action( 'woocommerce_after_shop_loop_item_title' );

#rest of the content-product.php...

我怎样才能做到这一点?

谢谢

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

做你期望的最好的方法(没有覆盖模板)是使用我们挂钩在'woocommerce_shop_loop_item_title'钩子的函数。

在这种情况下,下面的代码会出现在您的活动子主题(或主题)的function.php文件中。

这是代码:

// For WooCommerce below version 3.0
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 ) {

            // Getting the value of an attribute
            $attribute_value = array_shift(wc_get_product_terms( $product->id, $attribute_name));

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

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

对于woocommerce 3.0+,请参阅:Add Attributes to Short Description in WooCommerce 3.0+

因此,您将在标题之后,在产品类别档案页面上获得您期望的内容。


或者您可以使用模板content-product.php中的上述代码,这样:

#content-product.php start.... 

do_action( 'woocommerce_shop_loop_item_title' );

/////////////// HERE IS THE CODE ////////////////

// 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 ) {

        // Getting the value of an attribute
        $attribute_value = array_shift(wc_get_product_terms( $product->id, $attribute_name));

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

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


/**
 * woocommerce_after_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_rating - 5
 * @hooked woocommerce_template_loop_price - 10
 */
do_action( 'woocommerce_after_shop_loop_item_title' );

#rest of the content-product.php...

但第一个解决方案更值得推荐和优雅。

代码经过测试和运行

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