Adding custom attribute to WooCommerce shop loop product title

前端 未结 1 604
遇见更好的自我
遇见更好的自我 2021-01-28 01:12

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_a         


        
1条回答
  •  不知归路
    2021-01-28 02:06

    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!

    0 讨论(0)
提交回复
热议问题