1

I'm working on something on wordpress + woocommerce. I've inputed some 'filter' attributes value for some products.

So if the value of the attribute for this product includes a "Wood Grains" value, i want to echo an Woodgrains.jpg on the front end.

Therefore if the value = Texture, i want to echo Texture.jpg icon.

the code below is what i've mustered so far, but this only echoes out all the values tagged to a product, i can't figure out what to change to get the 'if' statement in it.

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

here's a screenshot of what the code above does on the front end: http://edleuro.com/new/wp-content/themes/mystile/img/1.png

3 Answers 3

3

If this returns an array of terms for the said product:

$terms = get_the_terms( $product->id, 'pa_filter'); 

You can check if the returned result array has what you are looking for by doing this:

if (in_array("Wood Grains", $terms)) { // has it } else { // doesn't have it } 

Update

Based in your reply to my answer, I have came up with the following:

Create a helper function like this:

function termExists($myTerm, $terms) { if (is_array($terms)) { foreach ($terms as $id => $data) { if ($data->name == $myTerm) return true; } } return false; } 

Then you use it like this:

if (termExists('Wood Grains', get_the_terms($product->id, 'pa_filter'))) { // term exists } else { // does not exists } 
Sign up to request clarification or add additional context in comments.

8 Comments

sorry, so.. i tried but i'm not sure what's wrong. the one liner you wrote, is to replace the whole code i initially wrote above? am i correct? something like this? if (in_array("Wood Grains", get_the_terms($product->id, 'pa_filter'))) { echo "Have a good day!"; }
I've updated my post about the one liner. Yes; that's how you use it. Have you tried it yet?
yes i've tried with the updated one-liner. but it doesnt echo anything unfortunately. i'll try mess around with the code to see if anything comes out. thanks
Okay, ignore the one-liner. do this $terms = get_the_terms( $product->id, 'pa_filter'); and then check like I showed. If it still doesn't work, can you var_dump($terms); - i'd like to see it's contents.
didnt quite understand by your "do this..." anyway did a var dump. here's the output array(1) { [20]=> object(stdClass)#277 (11) { ["term_id"]=> int(20) ["name"]=> string(11) "Wood Grains" ["slug"]=> string(11) "wood-grains" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(21) ["taxonomy"]=> string(9) "pa_filter" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(1) ["object_id"]=> int(13) ["filter"]=> string(3) "raw" } }
|
0

i found a better way to resolve my problem. i had this in my content-product.php woocommerce template.

<?php $terms = get_the_terms( $product->id, 'pa_filter'); foreach($terms as $term){ if($term->name == 'Wood Grains'){ echo '<img src="icon_woodgrains.gif">'; } } ?> 

do take note that the term name is case sensitive.

Comments

0

foreach prints Warning: invalid argument supplied for foreach()…

This works for me "on a purely":

if ( is_product() && has_term( 'Wood Grains', 'pa_filter' )) { echo '<img src="Texture.jpg">'; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.