This is how to display custom product attributes on a WooCommerce single product page. (To show attributes on the cart page, see Show WooCommerce Product Attributes on Cart Page.) This is done programmatically via your functions file. This uses a Woocommerce hook, âwoocommerce_single_product_summary
â, which will make the product attribute appear above the âadd to cartâ button. This goes in your functions.php
file, not inside a template file.
Here are 3 examples. Example 1 will show one product attribute of your choosing. Example 2 shows all custom product attributes for the product. Example 3 also shows all custom product attributes, but it puts them in an unordered list and adds CSS classes so you can style them.
Example 1: Show 1 Product Attribute
This example will display one product attribute. You must replace âSome Attribute Titleâ on line 7 with the title of your own product attribute (the title, not the slug!). By default, this will display the attribute label and the value, like so: Label: value. To show only the value, comment out lines 46 and 59 below.
(Instead, if you want to show 2 or more specific attributes, see this comment below.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | /** * WooCommerce: Show only one custom product attribute above Add-to-cart button on single product page. */ function isa_woo_get_one_pa(){ // Edit below with the title of the attribute you wish to display $desired_att = 'Some Attribute Title' ; global $product ; $attributes = $product ->get_attributes(); if ( ! $attributes ) { return ; } $out = '' ; foreach ( $attributes as $attribute ) { $name = $attribute ->get_name(); if ( $attribute ->is_taxonomy() ) { // sanitize the desired attribute into a taxonomy slug $tax_slug = strtolower (trim(preg_replace( '/[^A-Za-z0-9-]+/' , '_' , $desired_att ))); // if this is desired att, get value and label if ( $name == 'pa_' . $tax_slug ) { $terms = wp_get_post_terms( $product ->get_id(), $name , 'all' ); // get the taxonomy $tax = $terms [0]->taxonomy; // get the tax object $tax_object = get_taxonomy( $tax ); // get tax label if ( isset ( $tax_object ->labels->singular_name ) ) { $tax_label = $tax_object ->labels->singular_name; } elseif ( isset( $tax_object ->label ) ) { $tax_label = $tax_object ->label; // Trim label prefix since WC 3.0 if ( 0 === strpos ( $tax_label , 'Product ' ) ) { $tax_label = substr ( $tax_label , 8 ); } } foreach ( $terms as $term ) { $out .= $tax_label . ': ' ; $out .= $term ->name . '<br />' ; } } // our desired att } else { // for atts which are NOT registered as taxonomies // if this is desired att, get value and label if ( $name == $desired_att ) { $out .= $name . ': ' ; $out .= esc_html( implode( ', ' , $attribute ->get_options() ) ); } } } echo $out ; } add_action( 'woocommerce_single_product_summary' , 'isa_woo_get_one_pa' ); |
Example 2: Show All Custom Product Attributes
Note that this does NOT affect nor include attributes which are used for Variations. This only includes âregularâ custom product attributes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | /** * Show all product attributes on the product page */ function isa_woocommerce_all_pa(){ global $product ; $attributes = $product ->get_attributes(); if ( ! $attributes ) { return ; } $out = '' ; foreach ( $attributes as $attribute ) { // skip variations if ( $attribute ->get_variation() ) { continue ; } $name = $attribute ->get_name(); if ( $attribute ->is_taxonomy() ) { $terms = wp_get_post_terms( $product ->get_id(), $name , 'all' ); // get the taxonomy $tax = $terms [0]->taxonomy; // get the tax object $tax_object = get_taxonomy( $tax ); // get tax label if ( isset ( $tax_object ->labels->singular_name) ) { $tax_label = $tax_object ->labels->singular_name; } elseif ( isset( $tax_object ->label ) ) { $tax_label = $tax_object ->label; // Trim label prefix since WC 3.0 if ( 0 === strpos ( $tax_label , 'Product ' ) ) { $tax_label = substr ( $tax_label , 8 ); } } $out .= $tax_label . ': ' ; $tax_terms = array (); foreach ( $terms as $term ) { $single_term = esc_html( $term ->name ); array_push ( $tax_terms , $single_term ); } $out .= implode( ', ' , $tax_terms ) . '<br />' ; } else { $out .= $name . ': ' ; $out .= esc_html( implode( ', ' , $attribute ->get_options() ) ) . '<br />' ; } } echo $out ; } add_action( 'woocommerce_single_product_summary' , 'isa_woocommerce_all_pa' , 25); |
Example 3
This is just like Example 2, but with added CSS selectors for easy styling. Another difference is that this is output in an HTML unordered list. See below for how to show terms as links.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | function isa_woocommerce_all_pa(){ global $product ; $attributes = $product ->get_attributes(); if ( ! $attributes ) { return ; } $out = '<ul class="custom-attributes">' ; foreach ( $attributes as $attribute ) { // skip variations if ( $attribute ->get_variation() ) { continue ; } $name = $attribute ->get_name(); if ( $attribute ->is_taxonomy() ) { $terms = wp_get_post_terms( $product ->get_id(), $name , 'all' ); // get the taxonomy $tax = $terms [0]->taxonomy; // get the tax object $tax_object = get_taxonomy( $tax ); // get tax label if ( isset ( $tax_object ->labels->singular_name ) ) { $tax_label = $tax_object ->labels->singular_name; } elseif ( isset( $tax_object ->label ) ) { $tax_label = $tax_object ->label; // Trim label prefix since WC 3.0 if ( 0 === strpos ( $tax_label , 'Product ' ) ) { $tax_label = substr ( $tax_label , 8 ); } } $out .= '<li class="' . esc_attr( $name ) . '">' ; $out .= '<span class="attribute-label">' . esc_html( $tax_label ) . ': </span> ' ; $out .= '<span class="attribute-value">' ; $tax_terms = array (); foreach ( $terms as $term ) { $single_term = esc_html( $term ->name ); // Insert extra code here if you want to show terms as links. array_push ( $tax_terms , $single_term ); } $out .= implode( ', ' , $tax_terms ); $out .= '</span></li>' ; } else { $value_string = implode( ', ' , $attribute ->get_options() ); $out .= '<li class="' . sanitize_title( $name ) . ' ' . sanitize_title( $value_string ) . '">' ; $out .= '<span class="attribute-label">' . $name . ': </span> ' ; $out .= '<span class="attribute-value">' . esc_html( $value_string ) . '</span></li>' ; } } $out .= '</ul>' ; echo $out ; } add_action( 'woocommerce_single_product_summary' , 'isa_woocommerce_all_pa' , 25); |
In Example 3, you can show the terms as links by inserting the following code into line 45. Note that terms as links will only be shown for global attributes.
// Show terms as links, when available if ( $single_product ) { $term_link = get_term_link( $term ); if ( ! is_wp_error( $term_link ) ) { $single_term = '<a href="' . esc_url( $term_link ) . '">' . esc_html( $term ->name ) . '</a>' ; } } |
Monte
April 18th, 2014 at 4:16 pm
Thanks Isabel, this was super helpful. There seems to be a lack of great documentation for WooCommerce around the web and your post was quite helpful.
Tilen
April 29th, 2014 at 6:33 pm
Hi Isabel, what a great and informative post. Been searching for this for the past hour and now finally landed here.
Iâm looking for a similiar code, with the main difference being that, instead of using it on the product page, Iâd like to get the product attributes on the cart page under the product title. Can this also be done with the above code?
Thank you.
Isabel
May 2nd, 2014 at 2:19 pm
Thank you. Please see, Show WooCommerce Product Attributes on Cart Page.
Tilen
May 7th, 2014 at 11:57 am
I.. I love you.
Kim
April 29th, 2014 at 11:22 pm
Hi Ms. Isabel, I use woocommere in my site. Did you see the attribute on every single product page(example: Language : Select Language)
I wanted the select language under the label language. How can i do that?
Please follow the link to see the site. Thanks Ms. Isabel!
http://www.mycitytourapp.com/product/adelaide/
Isabel
May 2nd, 2014 at 12:34 pm
If you want the visitor to be able to select a language, then you want to add a âVariationâ. For âProduct Dataâ, select âVariable productâ. Then youâll see the âVariationsâ tab. In there, click âAdd Variationâ. See WooCommerce docs regarding Product Variations.
Nick
May 1st, 2014 at 10:17 am
Isabel, this is great. Since Iâm a noob, I hope you can help me figure out how to make this work for multiple attributes. I had success with the old in template version, but had the same issue where I could only call one attribute for the category.
This is what I was working with:
$versionvalues
= get_the_terms(
$post
->id,
'pa_available-finish'
,
'pa_available-vanity-finishes'
);
if
(
is_array
(
$versionvalues
)) {
foreach
(
$versionvalues
as
$versionvalue
) {
echo
$versionvalue
->name .
' '
;
}
}
But the problem was with âpa_available-finishâ, âpa_available-vanity-finishesâ: How can I pull multiple attributes?
If there is a better way to do this in functions.php, how do I insert the results in the template where I want them to appear?
PS: the works ok but I couldnât figure out how to make an instead.
Thank you so much!
Nick
May 1st, 2014 at 10:23 am
Update, writing it out got me thinking differently. I just repeated the code for each attribute and it appears to work that way. Good enough to get by for now. But would be interested in learning if there is a better way to make this happen.
Thanks!
Isabel
May 2nd, 2014 at 2:24 pm
The second example, âShow All Custom Product Attributesâ does just that. It should work for you.
George
July 2nd, 2014 at 4:30 am
Thanks a lot for this snippet Isabel!
I have a problem though and if anybody can help Iâd really appreciate it.
At the if statement where you check:
if ( $attribute[âis_taxonomyâ] )
the check is true and the 1st branch is executed.
I wonder, why this happens, since my wp is 3.9.1, woocommerce is updated to 2.1.12 and your comment says that the 1st branch is for backwards compatibility.
Also, it seems like the snippet needs some modifications in order to work well with WPML. If anybody is interested on this, check this support ticket here:
http://wpml.org/forums/topic/show-translated-woocommerce-custom-product-attributes-with-php-functions/
Thanks again!
George
Thomas
July 31st, 2014 at 10:46 pm
Progress! It shows now âpa_seed-type: Feminizedâ!
Any idea on how to get that âpa_seed-typeâ to just âSeed Typeâ? Then it would fully work!
Thank you soooooo much!!!
Thomas
August 3rd, 2014 at 8:43 pm
Bingo, we got a winner! It works now perfectly!
Thank you very much for your help, I am really happy!
This snippet is a real timesaver, and would be a very valuable plugin when combined with a tab creator which can show video, images, text and such. Adding the possibility to turn on/off the breadcrumbs, SKU, categories and tags, and the option to remove tab titles/heading would also be easy to implement by a skilled person like you.
And when you can add the feature with the option to select to show the attributes in the additional information tab, on top of the price or both, would make it even more useful. For the WC custom tabs pro plugin (I use the lite version) they charge $99, which is a lot for its limited use IMO.
Thanks again for you help to make that amazing snippet work on my site!
Marcus
August 5th, 2014 at 8:01 pm
Thanks for this excellent action and the great instructions!
Using âExample 2: Show All Custom Product Attributes,â can you advise on a way to do two things:
1) display the custom product attributes in some sort of organized fashion (for example: instead of the basic output with the BR tag between each title/attribute pair, maybe output them to a unordered list or a definition list)
2) wrap the individual attributes in unique classes so that they can each be styled separately.
For example, I have products that are made up of differing colors. These colors are not variations, they are just descriptions of the colors within the products themselves. Iâd like to be able to automatically wrap them with a unique class or ID, such as:
`red, green, blue`
It doesnât have to be spans; thatâs just an example. But something that would let us apply a class string with something like âattribute-title attribute-valueâ
Thanks again!!!
Isabel
August 7th, 2014 at 2:03 am
Your welcome. Iâm sorry, I canât see your code. Backticks donât properly escape all HTML code. If you want me to see your code, please wrap HTML in bracket tags like this:
I just added a new âExample 3â above. See if that meets your needs. Hope that helps.
Marcus
August 7th, 2014 at 8:27 pm
My apologies for mucking up your comments section with my attempts to add code.
THANK YOU for the great Example 3 â that looks awesome. Iâll play with it and report back if I have anything helpful to add!
Andreas
August 10th, 2014 at 3:34 am
Dear Isabel,
i have installed your Plugin from WordPress.org but it wont work with my âSatellite7â theme. I need to see Attributes on the Cart Page and Order Informations. Can u help me please?
btw i have no idea from Coding.
PS: excause my bad english, iam from GermanyâŠ
Isabel
August 10th, 2014 at 8:47 pm
Hello,
Originally, the plugin only displayed the attributes on the âsingle product pageâ. The plugin was just updated to version 1.2, which now shows the attributes also on the Cart Page, the Order Details page, and in the order emails.
What doesnât work on your theme? Did the attributes at least appear on the âsingle product page?â If not, then a link to the site would help me to investigate the problem. I hope this helps.
Andreas
August 11th, 2014 at 1:30 am
Hey Isabel,
you are awesome! With the Update to version 1.2 the problem was fixedâŠ! Thanks a lotâŠ
and Greetings from Germany
btw yor Name sounds a little italian. Iam an half Italian, my Father is from Scily. Maybe u are about this a little awesomeâŠ
thank you againâŠ
Ali Milner
September 3rd, 2014 at 3:40 pm
Hi Isabel.
I am trying to display my attributes not only pn the cart, single product page but would like to display the attributes were ever the product name is displayed. I am building a site to sell sheet music and need to add the composer and arranger name. http://www.fortonmusic.co.uk/forton/product-category/saxophone/saxophone-quintet-ensemble/
if you check out the link i need to add the names under the title before the price.
Hope this makes sense!!!!
Thank you
Ali
Isabel
October 12th, 2014 at 8:07 pm
This option will be added in the next update of the plugin.
Neil
October 8th, 2014 at 6:40 am
Hi Isabel,
Great code, how do you determine where the attributes are displayed on the page. I want them after the description and canât figure it out?!
Thanks
Isabel
October 8th, 2014 at 11:23 am
Thank you. I responded here:
https://wordpress.org/support/topic/repositioning-display-of-attributes?replies=2
pavel
October 13th, 2014 at 5:34 am
Hello Isabel,
thank you for your great job! It is working fine (I am using example 2) and I would like to ask you, if there is possibility to show attribute as a link to its archive page. I know how to make archive page for attributes (as I follow woocommerce docs) and I have found a code snippet which should make it possible https://gist.github.com/coenjacobs/2594985 (actually that snippet is not workig for me at all, so I am using your code bcause it is working like a charm). Can I implement the part for getting attribute url to your code somehow? I should be this part:
if
( !
empty
(
$terms
) ) {
foreach
(
$terms
as
$term
) {
$archive_link
= get_term_link(
$term
->slug,
$attribute_name
);
$full_line
=
'<a href="'
.
$archive_link
.
'">'
.
$term
->name .
'</a>'
;
array_push
(
$terms_array
,
$full_line
);
}
echo
$taxonomy
->labels->name .
' '
. implode(
$terms_array
,
', '
);
}
Thank you.
Mike Smith
February 3rd, 2015 at 4:10 am
Hi Isabel,
Thanks so much for this code â itâs really helpful. Might there be a way to manually transform the attributes into links? If you have a limited number of attributes, could you use if statements? EG something vaguely like:
if
(
$attribute
[
'value'
] =
'orange'
) {
$out
.=
'<a href="'
./shop/?filter_attribute-color=81 .">';
$out
.=
'<li class="'
. sanitize_title(
$attribute
[
'name'
]) .
' '
. sanitize_title(
$attribute
[
'value'
]) .
'">'
;
$out
.=
'<span class="attribute-label">'
.
$attribute
[
'name'
] .
': </span> '
;
$out
.=
'<span class="attribute-value">'
.
$attribute
[
'value'
] .
'</span></li>'
;
$out
.=
'</a>'
}
?
Dave
November 26th, 2014 at 1:11 am
For some reason my color attributes still arenât showing even when I have them set to be âVisible on product pageâ. Any thoughts as to why? Hereâs my site: http://williambandfriends.com/shop/
I really appreciate your time and help! Youâre literally the only person with this type of snippet that works, so thank you!
Andrey
November 27th, 2014 at 7:23 am
Nice codes. I very need the help for the single-product page, it is required to put the table as here (in variable):: http://www.eggplantstudios.ca/woocommerce-product-variation-add-cart-grid/
Marcus
January 10th, 2015 at 11:03 am
Awesome code, Isabel!
Iâm trying to replace attribute values with images/icons so the specific styles set to each attribute value are great.
In looking at Example 3 above, the output is such that attribute values of the same attribute type are listed separately.
So, they display as:
<
ul
>
<
li
>Color: Red</
li
>
<
li
>Color: Green</
li
>
<
li
>Color: Blue</
li
>
</
ul
>
However, is there are way to make them display as
<
ul
>
<
li
>Color: Red, Green, Blue</
li
>
</
ul
>
since theyâre all part of the same attribute group?
But, since I want to replace each of those color name values with an image/icon, the style of the attribute value (such as âredâ) thatâs currently applied to the LI class would need to be applied to the value span itself.
Is this possible?
Sam
November 3rd, 2016 at 5:08 pm
I need to do something similar. I only want the different attributes separated by a comma. While I got this working a little by editing line number 47 to this:
$out .= '<span class="attribute-value">' . $term->name . ', ' . '</span>';
This puts a comma after the attribute values, even if there is just one attribute.
Is there a way I can create a loop where the comma will show if there are more than 1 attribute and donât show the comma if there is only one attribute?
Isabel
March 23rd, 2017 at 12:08 am
To do this, see this comment.
DHEERAJ Singh
November 4th, 2016 at 10:44 pm
Hi were you able to achieve this?
Andrey
January 11th, 2015 at 2:58 am
I very need the help for the simple function of product page, it is required to put the table as here (in variable):: http://www.eggplantstudios.ca/woocommerce-product-variation-add-cart-grid/
Maze Creative
March 31st, 2015 at 6:50 am
I was having a very specific problem using this method. I was using the top function to display the size numbers of clothing in a client site. However, due to the bizarre numerical ordering of WP, it was placing sizes 6 and 8 after 10, 12, 14 etc. The sizes were displaying as â10, 12, 14, 16, 6, 8âČ, rather than â6, 8, 10, 12, 14, 16âČ. It sees the 1 at the start of 10 and thinks that is first. Custom ordering wasnât taking effect either.
To get around this, I added a 0 to the start of sizes 6 and 8, so they became â06â and â08â. Then, I changed the slugs of the sizes to words rather than numbers, six eight ten etc, so that I could use these as CSS classes. I then used the ::first-letter pseudo element (https://css-tricks.com/almanac/selectors/f/first-letter/) to hide the â0â at the start of attributes 06 and 08.
As I say, very specific, but a quick solution that will hopefully will help someone.
Ernesto
August 28th, 2015 at 4:31 pm
Did you tried edit the âdefault sort orderâ in the attribute settings?
Ernesto
August 28th, 2015 at 4:28 pm
Hello Isabel,
I was wondering if there is a way to show the attribute description or slug of the attribute.
http://i.imgur.com/9ooiMmK.jpg
Long story short: I need to assemble a SKU code based in the attribute selection.
Thanks in advance!
Isabel
September 1st, 2015 at 10:53 am
Hi,
You can show the attribute description by inserting this in Example 1, line 48:
$out
.=
$term
->description .
'<br />'
;
Hope that helps.
Jeff Cohan
May 12th, 2016 at 4:44 pm
Excellent, and thanks so much, Isabel!
I slightly modified your Example #1 code to convert a predefined URL product attribute value into a clickable link. It works marvelously!
foreach ( $terms as $term ) {
$out .= $tax_label . ': ';
//$out .= $term->name . '<br />';
$out .= '<a target="_blank" href="' . $term->name . '">Web Site</a><br />';
}
A minor point:
Your sanitizing line assumes that the tag for the attribute name is the lowercased version of the attributeâs name with underscores as the separator. In my case, I used hyphens as the separator, and (for some silly reason) my title and slug used slightly different characters. I changed the â_â to a â-â in the code, and I changed the slug to make the sanitizing work.
Thanks again!
Sam
October 23rd, 2016 at 9:27 pm
If someone is using example 1 to show one product attribute and wants to show that attribute on the Shop/category page add the following code at the end of the code in example 1.
add_action('woocommerce_shop_loop_item_title', 'isa_woo_get_one_pa');
Sheshraj Ghimire
January 23rd, 2017 at 10:47 am
Hello!
This is a wonderful code that I can use to manipulate the placement of different attributes at different location. I want to use the codes in example 1 and output only certain attributes. However, as per example 1, I can only output one attributeâs name and value. Is there a way I can output more that one attribute? I donât want to show all attributes as in example 2 or 3 and want to control what to output and what not to.
Ramses
March 12th, 2017 at 6:41 pm
hello. great article very usefull.
Is there any way to find the id of each attribute using this same function? letâs say $term->id or something like that.
Isabel
March 14th, 2017 at 10:34 pm
Thanks. It depends on which example youâre using, but for the regular $term object, you can get the ID with this:
$term->term_id
Elena
March 29th, 2017 at 5:39 am
Hi Isabel! Your article are very helpful and I used the code here. But I have a problem Iâm struggling with this for weeks and I canât find the answer. So this is it:
When you go to Attributes â> add a new attribute â> configure terms you can also add children terms.
For example I can add an attribute Shape and its terms would be Square (with the children-terms 10Ă10, 15Ă15), Rectangle (with children-terms 10Ă15, 10Ă20).
How can I get those, the children-terms ?
Isabel
April 3rd, 2017 at 7:57 pm
I tried the 3 code examples on this page with âchildren termsâ and also with a âgrandchild termâ and it does show those children. Do you mean that youâre using a different code and want to get the child terms?
Patrick
March 29th, 2017 at 2:05 pm
Hello,
How can i have attributes show on the single product page but not on the product category page?
Michael
April 5th, 2017 at 1:42 am
This is great, thank you so much for these snippets,. Iâve been looking everywhere I could and finally found it! I wanted to know tho how to do just 2 attributes instead of 1 or all? Thanks again for sharing your work!
Isabel
April 6th, 2017 at 4:28 pm
Your welcome. Sure, to show 2, use the following. Replace the attribute titles on line 7. On that line, you can add as many attributes as desired, wrapped within quotes, and separated by a comma.
/**
* Show some, specified, WooCommerce product attributes above Add-to-cart button on single product page.
*/
function
isa_woo_get_some_pa(){
// Edit below with the title of the attribute you wish to display
$desired_atts
=
array
(
'Some Attribute Title'
,
'A Second Attribute Title'
);
// sanitize attributes into taxonomy slugs
foreach
(
$desired_atts
as
$att
) {
$tax_slugs
[] =
strtolower
(trim(preg_replace(
'/[^A-Za-z0-9-]+/'
,
'_'
,
$att
)));
}
global
$product
;
$attributes
=
$product
->get_attributes();
if
( !
$attributes
) {
return
;
}
$out
=
''
;
foreach
(
$attributes
as
$attribute
) {
$name
=
$attribute
->get_name();
if
(
$attribute
->is_taxonomy() ) {
$clean_name
=
$attribute
[
'name'
];
// Trim pa_ prefix
if
( 0 ===
strpos
(
$clean_name
,
'pa_'
) ) {
$clean_name
=
substr
(
$clean_name
, 3 );
}
// if this is a desired att, get value and label
if
( in_array(
$clean_name
,
array
(
$tax_slugs
) ) ) {
$terms
= wp_get_post_terms(
$product
->get_id(),
$name
,
'all'
);
// get the taxonomy
$tax
=
$terms
[0]->taxonomy;
// get the tax object
$tax_object
= get_taxonomy(
$tax
);
// get tax label
if
( isset (
$tax_object
->labels->singular_name ) ) {
$tax_label
=
$tax_object
->labels->singular_name;
}
elseif
( isset(
$tax_object
->label ) ) {
$tax_label
=
$tax_object
->label;
// Trim label prefix since WC 3.0
if
( 0 ===
strpos
(
$tax_label
,
'Product '
) ) {
$tax_label
=
substr
(
$tax_label
, 8 );
}
}
$out
.=
$tax_label
.
': '
;
$tax_terms
=
array
();
foreach
(
$terms
as
$term
) {
$single_term
= esc_html(
$term
->name );
array_push
(
$tax_terms
,
$single_term
);
}
$out
.= implode(
', '
,
$tax_terms
) .
'<br />'
;
}
// our desired att
}
else
{
// for atts which are NOT registered as taxonomies
// if this is desired att, get value and label
if
( in_array(
$name
,
array
(
$desired_atts
) ) ) {
$out
.=
$name
.
': '
;
$out
.= esc_html( implode(
', '
,
$attribute
->get_options() ) ) .
'<br />'
;
}
}
}
echo
$out
;
}
add_action(
'woocommerce_single_product_summary'
,
'isa_woo_get_some_pa'
);
Lisa
May 4th, 2017 at 10:07 pm
Hi Isabel
Youâre freakinâ awesome â thanks so much for this code!
Iâm in Woo 3.0.5 and examples 1, 2, 3 all work for me ⊠however the code in this comment doesnât. Since Iâm not a coder, Iâm hoping you have a suggestion?
Ultimately need this code â multiple, but not all attributes â plus earlier comment for âColor: Red, Green, Blueâ.
Thank you in advance for any help you can offer
Isabel
June 5th, 2017 at 2:57 pm
Thank you! This code block (in the comment) has just been updated.
Juan RamĂłn
May 6th, 2017 at 10:01 pm
Hello. I canât get work the code to show 2 attributes (WP 4.7.4 & WC 3.0.5).
Also, maybe that code (if itâs ok) can be added to the main post, Itâs very useful.
Thanks for your work!
Isabel
June 5th, 2017 at 3:00 pm
Thank you! This code block has just been updated.
Nico
April 6th, 2017 at 9:16 am
Hello,
have an issue with the code above and WooCommerce 3.0.
Before each attribute-label, the word âproductâ ist written. Do you know how i can fix that?
Thank you very much.
Isabel
April 6th, 2017 at 4:06 pm
I just updated the 3 examples on this page for WooCommerce 3.0. Hope that helps.
Paolo
April 14th, 2017 at 11:52 am
Hello,
the code of Example 1 (updated for WooCommerce 3.0), also it works with previous versions of WooCommerce?
I ask this because donât work with WooCommerce 2.6.14.
Thanks.
Paolo
April 14th, 2017 at 1:48 pm
Errata corrige: it works perfectly.
The name of my attribute was incorrect âProduttore:â I changed it to âProduttoreâ (without â:â).
Is possible to change the position under the short description?
Now itâs displayed under the price:
https://www.icuginitoccasana.bio/icugini2017/prodotto/scaglie-di-sapone-di-marsiglia-980-g/
Thanks.
Isabel
April 17th, 2017 at 2:29 pm
Sorry, the updated code on this page doesnât work with older WooCommerce versions below 3.0.
zygimantas
June 1st, 2017 at 8:03 am
Hi I use your 3rd example, but it shows only the value but not the label, also not showing in the spans, Iâve got newest woocomerce version. thanks in advance
Isabel
June 5th, 2017 at 3:17 pm
Hi. This code has just been updated.
Daniel
June 3rd, 2017 at 10:36 am
Hi Isabel
I have an attribute called âUseâ, including the values âIndoorâ and âOutdoorâ
I want to add a custom image/icon to each of those attributes, and then display them on the product page (e.g. product with attribute Outdoor will display the Outdoor icon)
I know there is WooCommerce Variation Swatches, however these attributes are static/global (not used for variations)
How do I do that? Or do you know of a plugin that offers this feature?
Isabel
June 5th, 2017 at 5:05 pm
This can be done with Example 3. You would have to add some JavaScript to switch the text with images. For example, you can do it by adding the following to your functions. In the following code, replace the image URL on line 20 with the URL for your Indoor image/icon. Replace the image URL on line 26 with the URL for your Outdoor image/icon. (Note that the ID for your attribute,
pa_use
, is on line 9. You can edit that, as needed.)/**
* Insert JavaScript to replace WooCommerce product attributes text with images.
*/
function
my_woocommerce_attributes_images(){
?>
<script>(
function
() {
var
list, index, element, attValue, imgURL;
list = document.getElementsByClassName(
"pa_use"
);
for
(index = 0; index < list.length; ++index) {
element = list[index];
attValue = element.getElementsByClassName(
"attribute-value"
)[0].innerText;
switch
(attValue.trim()) {
case
"Indoor"
:
imgURL =
"http://icons.iconarchive.com/icons/elegantthemes/beautiful-flat/48/door-icon.png"
;
break
;
case
"Outdoor"
:
imgURL =
"http://icons.iconarchive.com/icons/paomedia/small-n-flat/48/flower-icon.png"
;
break
;
}
element.innerHTML =
'<img src="'
+ imgURL +
'" width="48" height="48" />'
;
}
})();</script>
<?php
}
add_action(
'wp_footer'
,
'my_woocommerce_attributes_images'
);
Daniel
June 5th, 2017 at 7:35 pm
Hi Isabel
Works like a charm!
Two questions:
1. How do I display multiple icons? (e.g. if the product has both Indoor&Outdoor)
2. How do I display specific attributes?
Thanks a lot!
Fernando
March 24th, 2018 at 9:52 am
Hi Isabel
Really nice functions, thanks a lot for posting these solutions. Implementation is very good
Thanks again