How to pull custom attributes for product on shop/category page (11 posts)

  1. oksanaar
    Posted 3 years ago #

    Hey there!

    So, I'd like to display custom attributes under the product title on the shop page (beside just the price and the title).

    I've been recommended the following:

    ...from within the WooCommerce cart contents loop (the $_product variable is then available to you) you can use the 'get_attributes( 'attribute-slug' )' function to retrieve any product attribute, where 'attribute-slug' is any attribute slug, like so:

    '<?php echo $_product->get_attribute( 'attribute-slug' );?>'

    Thanks for forgetfuljames. But it does not work for me , unfortunately. I put it right after the title, as in
    ' <h3><?php the_title(); ?></h3>
    <?php echo $_product->get_attribute( 'budget' );?>'

    but it only shows me Fatal error: Call to a member function get_attribute() on a non-object in

    What worked is when I copied the whole product-attribute.php file content into the content-product.php (relevant place). But I'm sure it's too basic to do it this way.

    Is there any way to do the filter/hook thing there? Or do the previous way, but fix whatever is not working for me?

    Thank you so much for the response!


  2. gruffalos
    Posted 3 years ago #

    I'd like to do this as well, did you find out anything else?

  3. oksanaar
    Posted 3 years ago #

    Nope. Unfortunatley not. I actually ended up using a difference ecommerce plugin - The CartPress. And after fiddling with it, scrapped it away also, and just did my own custom type. FYI, I didn't need full ecommerce functionality to start with.

  4. gruffalos
    Posted 3 years ago #

    Ok, thanks for such a quick reply!
    I'll try using a custom type :)

  5. TinaHunter
    Posted 3 years ago #

    I just figured this out for myself.

    After the <h3> Title, in the "content-product.php" enter this:

    <p class="YOURS"><?php
    $subheadingvalues = get_the_terms( $product->id, 'pa_subheading');
          foreach ( $subheadingvalues as $subheadingvalue ) {
           echo $subheadingvalue->name;

    Replace all of the "subheading" with the name of your custom attribute, and use your own class to style it as you need.

    The only down side is that every product must have something in this custom field otherwise you get mini error codes underneath each product. I've yet to find an if statement that works (will post again if I do... or someone else can pitch in).

    Hope this helps someone.

    PRO TIP: make sure you copy and paste this file into your theme folder first, and make your changes to that copy, so that they won't get overwritten when you update the plugin.

  6. ram.developer89
    Posted 3 years ago #

    I want category specific attribute for product, any suggestion????

  7. blueraccoon
    Posted 3 years ago #

    That is great Tina, works perfectly, many thanks.

    Does anyone know how to add the stock quantity to the shop/category page also?

  8. demetrioman
    Posted 2 years ago #

    Great, but if a product has no attribute, I’ve got the error:

    Warning: Invalid argument supplied for foreach() in […]/www/wp-content/themes/neighborhood/woocommerce/content-product.php on line 172

    How can be solved?

  9. sagalbot
    Posted 2 years ago #

    @demetrioman, you just need to wrap your foreach in a conditional:

    $terms = get_the_terms( $post->ID, 'pa_subheading' );
    if ( $terms && ! is_wp_error( $terms ) ) : 
    	foreach ( $terms as $term ) {
    		do your stuff here.
    endif; ?>
  10. alexsr
    Posted 2 years ago #

    Hey sagalbolt, thanks so much for that code snippet. Do you happen to know how I would phrase a conditional statement in which if there is more than one term for a product, then don't do anything?
    Basicall if the product is variable, I don't want it showing up on the page.


  11. Glennbosch
    Posted 2 years ago #

    Thanks to you all works perfect, well done

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic