WordPress.org

Support

Support » Plugins and Hacks » How to pull custom attributes for product on shop/category page

How to pull custom attributes for product on shop/category page

  • 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!

    http://wordpress.org/extend/plugins/woocommerce/

Viewing 10 replies - 1 through 10 (of 10 total)
  • I’d like to do this as well, did you find out anything else?

    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.

    Ok, thanks for such a quick reply!
    I’ll try using a custom type 🙂

    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;
            }
    ?></p>

    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.

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

    That is great Tina, works perfectly, many thanks.

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

    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?

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

    <?php
    $terms = get_the_terms( $post->ID, 'pa_subheading' );
    
    if ( $terms && ! is_wp_error( $terms ) ) : 
    
    	foreach ( $terms as $term ) {
    		do your stuff here.
    	}
    
    endif; ?>

    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.

    Thanks!

    Thanks to you all works perfect, well done

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to pull custom attributes for product on shop/category page’ is closed to new replies.