向WooCommerce商店循环产品标题添加自定义属性 [英] Adding custom attribute to WooCommerce shop loop product title

查看:148
本文介绍了向WooCommerce商店循环产品标题添加自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向WooCommerce循环添加自定义属性.目前,我的功能文件中包含以下内容

I am attempting to add a custom attribute to the WooCommerce loop. Currently I have the following in my functions file

function cc_template_loop_product_custom_attribute()
{
    $abv = $product->get_attribute('pa_alcohol-by-volume');
    if (!empty($abv))
    {
        echo get_attribute('pa_alcohol-by-volume');
    };
}

add_action('woocommerce_shop_loop_item_title', 'cc_template_loop_product_title', 10);

目的是在产品标题后显示体积酒精度"属性.但是,这不起作用,并且基本上会导致循环在到达函数后立即停止渲染.

The intention is to get display the attribute 'Alcohol by volume' after the product title. However this is not working and basically causing the loop to stop rendering as soon as it reaches the function.

推荐答案

直接调用get_attribute()会引发类似错误

Calling get_attribute() directly will throw an error like

调用未定义的函数get_attribute()

Call to undefined function get_attribute()

所以以这种方式使用

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

function wh_insertAfterShopProductTitle()
{
    global $product;

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

代码进入活动子主题(或主题)的 functions.php 文件中.或在任何插件php文件中.
代码已经过测试并且可以正常工作.

希望这会有所帮助!

这篇关于向WooCommerce商店循环产品标题添加自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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