WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. oksanaar
    Member
    Posted 1 year 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!

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

  2. gruffalos
    Member
    Posted 1 year ago #

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

  3. oksanaar
    Member
    Posted 1 year 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
    Member
    Posted 1 year ago #

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

  5. TinaHunter
    Member
    Posted 11 months 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;
            }
    ?></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.

  6. ram.developer89
    Member
    Posted 11 months ago #

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

  7. blueraccoon
    Member
    Posted 11 months 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
    Member
    Posted 8 months 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
    Member
    Posted 6 months ago #

    @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; ?>
  10. alexsr
    Member
    Posted 5 months 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.

    Thanks!

  11. Glennbosch
    Member
    Posted 5 months ago #

    Thanks to you all works perfect, well done

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic