UntitledGraphic
UntitledGraphic

Reputation: 1274

Adding custom attribute to WooCommerce shop loop product title

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.

Upvotes: 0

Views: 5369

Answers (1)

Raunak Gupta
Raunak Gupta

Reputation: 10809

Calling get_attribute() directly will throw an error like

Call to undefined function get_attribute()

So use it in this way

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

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works.

Hope this helps!

Upvotes: 3

Related Questions