WordPress.org

Forums

WooCommerce - excelling eCommerce
[resolved] Display custom attributes on Description tab of single product page (11 posts)

  1. jmek
    Member
    Posted 2 years ago #

    How do you call a custom attribute (called "author") in a template? I want to have it display on the Description tab of my single product page rather than just in the table of the Additional Information tab.

    I have already successfully made other customzations by copying the entire single-product directory from wp-content/plugins/woocommerce/templates to wp-content/themes/my-child-theme/woocommerce. All I need is the code snippet to display the custom attribute for the current product.

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

  2. bheadrick
    Member
    Posted 2 years ago #

    call this function in your product description template:

    function get_author(){
    global $product;
    $attributes = $product->get_attributes();
    $attribute = $attributes['author']
    woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
    if ( $attribute['is_taxonomy'] ) {
    
    				return implode( ', ', woocommerce_get_product_terms( $this->id, $attribute['name'], 'names' ) );
    
    			} else {
    
    				return $attribute['value'];
    
    			}
    }
  3. jmek
    Member
    Posted 2 years ago #

    That isn't quite working for me...I may be messing up the associated PHP tags since the page renders until it hits that code and then stops.

    I did get a snippet from someone else and it is working. I include it here in case anyone else needs it:

    <?php
    $authorvalues = get_the_terms( $product->id, 'pa_author');
    
          foreach ( $authorvalues as $authorvalue ) {
           echo $authorvalue->name;
            }
    ?>
  4. juce33
    Member
    Posted 2 years ago #

    Hi can you please help me break down your code. I want to do the same sort of thing - show a custom attribute called 'colour' in a field in the shopping cart or anywhere at all.

    How would i go about changing your code? i've tried replacing the word author with colour but that's not working for me and i don't quite understand exactly what needs to be done?

  5. jmek
    Member
    Posted 2 years ago #

    I don't really know php, I'm just using a snippet that someone else provided in another forum, so I can't really break this down for you.
    A couple of random possibilities: Did you change $authorvalue and $authorvalues to be $colourvalue and $colourvalues as well as changing pa_author to be pa_colour? And are you sure your attribute uses the British spelling (colour) rather than the American spelling (color)?

    Hope you find an answer!

  6. bheadrick
    Member
    Posted 2 years ago #

    juce33 - that should be all you need to do. woocommerce attributes are custom taxonomies. the name of the taxonomy is 'pa_' followed by the name of the attribute, so for the colour attribute, the taxonomy name would be pa_colour

    It depends on where you're putting this code. you may need to add this before that code to make sure that the $product variable is accessible:

    global $product

    the other variable names don't matter, by the way. It could just as easily be:

    $terms = get_the_terms( $product->id, 'pa_author');
    
          foreach ( $terms as $term ) {
           echo $term->name;
            }
  7. fedematta
    Member
    Posted 2 years ago #

    I have to print in my table in cart.php 2 coloumns, each one with a single attribute (size and colour).
    by default both are printed under product’s title.
    i wrote:

    $terms = get_the_terms( $product->id, 'pa_taglie');
    	foreach ( $terms as $term ) {
    	echo $term->name;
    	}

    but it returns tihis error:
    Warning: Invalid argument supplied for foreach()
    any suggestion?

  8. themattroyal
    Member
    Posted 2 years ago #

    No expert but you should check that what you are passing to foreach is an array by using the is_array function:

    $terms = get_the_terms( $product->id, 'pa_taglie');
    if (is_array($allvalues)) {
    	foreach ( $terms as $term ) {
    	echo $term->name;
    	}
    }
  9. rajbir4all
    Member
    Posted 2 years ago #

    how to get single attribute Name and value

  10. Moses001
    Member
    Posted 2 years ago #

    I have the same issue.
    I use single product. Before WooCommerce update to 2.0 I could see each single product's attributes (features) in the 'additional information' tab in table format. Now I can't see any tabs and no table with a display of each products attributes (features).

    I would like to know the code I should use and where I should put the code so I can display each single products attributes in a table within each single products product page.

  11. xehyundai3s
    Member
    Posted 1 year ago #

    Dear all,

    I have 2 tag_ID ( aa & bb ) in pa_colour, and I only want to show aa in new custom page, anybody can help me?

    Many thank you

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic