在Woocommerce存档页面中的产品标题下显示特定的产品属性 [英] Display specific product attributes under product title in Woocommerce archive pages

查看:60
本文介绍了在Woocommerce存档页面中的产品标题下显示特定的产品属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce中,我想在商店页面上的产品标题下显示一些产品属性.该产品的属性为年份",型号"和机油".

In woocommerce, I would like to show some product attributes on shop page under the product titles. This product attributes are "year", "model" and "oil".

这是我现在拥有的:

add_action('woocommerce_shop_loop_item_title', 'wh_insertAfterShopProductTitle', 15);

function wh_insertAfterShopProductTitle()
{
    global $product;

    $abv = $product->get_attribute('pa_year');
    if (empty($abv))
        return;
    echo __($abv, 'woocommerce');
}

感谢您的帮助.

推荐答案

要在Woocommerce存档页面中的产品标题下显示年",型号"和油"产品属性作为商店,请使用以下命令:

To display "year", "model" and "oil" product attributes under the product titles in Woocommerce archive pages as shop, use the following:

add_action('woocommerce_shop_loop_item_title', 'display_attributes_after_product_loop_title', 15);
function display_attributes_after_product_loop_title(){
    global $product;

    $output = array(); // Initializing

    // The year
    if( $year = $product->get_attribute('pa_year') ){
        // Save the value in the array
        $output[] = $year; 
    }

    // The model
    if( $model = $product->get_attribute('pa_model') ){
        // Save the value in the array
        $output[] = $model;
    }

    // The type of oil
    if( $oil = $product->get_attribute('pa_oil') ){
        // Save the value in the array
        $output[] = $oil;
    }

    // Output
    if( sizeof($output) > 0 ){
        // Display product attributes coma separated values (you can change the separator by something else below).
        echo implode( ', ', $output);
    }
}

代码进入您的活动子主题(活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (active theme). Tested and works.

这篇关于在Woocommerce存档页面中的产品标题下显示特定的产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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