Reply 350279 to: Display Product attribute on archive loop item

Avatar: Mike Papadakis
Mike Papadakis
Participant
March 16, 2023 at 16:40

So for explanation I have an attribute called delivery and has 2 terms inside, Greece and Only Kalamata, every product has 1 terms of these 2, so I want to display the attribute value on the products archive (shop, category etc) before the title. I have used this code but it doesn’t work

add_action( ‘woocommerce_shop_loop_item_title’, ‘ecommercehints_show_product_attributes_on_arhive_loop_item’, 1, 0 );
function ecommercehints_show_product_attributes_on_arhive_loop_item() {
global $product;
$product = wc_get_product( get_the_id() );
$product_attributes = $product->get_attributes();
$color_id = $product_attributes[‘pa_delivery’][‘options’][‘0’]; // returns the ID of the term. You will need to change ‘pa_color’ to your taxonomy attribute ID.
$color_name = get_term( $color_id )->name;
$class= $color_id == 228 ? “greekcolor” : “deliverypa”;
echo ‘‘ . $color_name . ‘‘;
};

Go To The Whole Conversation In Topic
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.