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
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
}
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.
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">';
}
Related
I am facing a bit complex situation with my taxonomy terms. I have list of taxonomy terms.
Taxonomy (property-status):
--2018
--2019
--2020
--2021
--Coming Soon
my taxonomy have multiple terms, usually i select one term from the taxonomy to display which i use this code to get:
$status_terms = wp_get_post_terms( get_the_ID(), 'property-status');
if($status_terms) {
foreach ( $status_terms as $term ) {
echo $term->name;
}
}
which is working perfect for me, but now i have selected two taxonomy terms 2019 and coming soon. If both are selected i want to show only 2019 i don't want to show coming soon alongside 2019, but if only coming soon is selected then i want to show coming soon.
You can count the terms and filter them accordingly. This might be a little bit too verbose, but may do the trick:
$status_terms = wp_get_post_terms( get_the_ID(), 'property-status');
if($status_terms) {
// Get the term names only
$term_names = array_map(function($term) { return $term->name; }, $status_terms);
if ((count($term_names) > 1) && in_array('coming-soon', $term_names)) {
// More than one term and coming-soon. Filter it out
foreach ( $status_terms as $term ) {
if ($term->name != 'coming-soon') {
echo $term->name;
}
}
} else {
// Show everything
foreach ( $status_terms as $term ) {
echo $term->name;
}
}
}
Shorter solution:
if($status_terms) {
$many_terms = (count($status_terms) > 1);
foreach ( $status_terms as $term ) {
if ($many_terms) {
if ($term->name != 'coming-soon') {
echo $term->name;
}
} else {
echo $term->name;
}
}
}
I want to display text on a single brand page on Woocommerce.
I wrote this snippet on my functions.php but it doesn't seem to work:
function woo_brand_page_check() {
$brands = wp_get_post_terms( $post->ID, ‘product_brand’, array("fields" => "all") );
if (is_tax( 'product_brand' )) {
foreach( $brands as $brand ) {
if($brand == 'BRAND_NAME') {
echo 'Custom text ';
}
}
} else {
echo '';
}
}
add_action('woocommerce_before_shop_loop', 'woo_brand_page_check');
If you have any suggestions on how to implement this, please drop me a line!
Thanks in advance!
If your Brands are product attributes, the taxonomy should be automatically prefixed with 'pa_', so you should use pa_product_brand.
Double check if product_brand has a dash in place of the underscore (as usually happens), so it would become pa_product-brand
Also, you may parr the term in is_tax() instead of looping all terms, something like:
function woo_brand_page_check() {
if ( is_tax( 'pa_product_brand', 'BRAND_NAME' ) ) {
echo 'Custom text ';
}
}
add_action('woocommerce_before_shop_loop', 'woo_brand_page_check');
See: https://codex.wordpress.org/Function_Reference/is_tax
Cheers ;)
I have an array $categories and I would like to check if either 'computers' or 'laptops' are in the array.
I can do
$terms = wp_get_post_terms($post_id, 'product_cat');
foreach ($terms as $term) {
$categories[] = $term->slug;
}
if (in_array( 'computers', $categories)) {
//run a lot of code
}
if (in_array('laptops', $categories)) {
//run a lot of code
}
but is there a way to combine with an OR so I don't have to write out the code twice?
something like
if ((in_array( 'computers', $categories)) OR (in_array('computers-he', $categories))) {
//run a lot of code
}
I tried it but it doesn't work I get.
PHP Warning: in_array() expects parameter 2 to be array, null
1. Define
$categories = array();
before-
$terms = wp_get_post_terms( $post_id, 'product_cat' );
2. Use || like this:-
if(in_array('computers',$categories) || in_array('laptops',$categories)) {
//run a lot of code
}
Now full code will be:-
$categories= array();
$terms = wp_get_post_terms( $post_id, 'product_cat' );
foreach($terms as $term) {
$categories[] = $term->slug;
}
if(in_array('computers',$categories) || in_array('laptops',$categories)) {
//run a lot of code
}
a different approach:
$terms = wp_get_post_terms($post_id, 'product_cat');
foreach ($terms as $term)
{
if(in_array($term->slug, ['computers', 'laptops']))
{
//run a lot of code
break; //run the code once
}
}
I usually do this:
if (array_intersect($categories, ['computers', 'laptops'])) {
//run a lot of code
}
If one or both terms are in $categories, it returns the array with those terms, if none are in it, it returns an empty array which is falsy.
From you question, I guess you need to run code if there is at least one term of particular category. In such case you can use array_reduce and take advantage of short-circuit evaluation:
$criteria = [
'computers' => true,
'laptops' => true
];
$test = function ($carry, $term) use ($criteria) {
return $carry || isset($criteria[$term->slug]);
};
if (array_reduce($terms, $test, false)) {
echo 'Run a lot of code.';
}
Here is working demo.
I am using the following code to output a list of custom taxonomies:
<?php // Get terms for post
$terms = get_the_terms( $post->ID , 'status' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
print $term->slug ;
print $term->name;
// Get rid of the other data stored in the object, since it's not needed
unset($term);
}
}
?>
My question is, how can I add html to this loop? I have tried multiple methods such as:
echo '<button class="filter $term->slug" data-filter="$term->slug">$term->name</button>';
...but this either errors out, or doesn't print the required terms. My desired output of html would be:
<button class="filter term-slug" data-filter="term-slug">term-name</button>
Please try this way. It should work. You need to just print '".$term->name."'like this inside the HTML to identify the object variable.
<?php // Get terms for post
$terms = get_the_terms( $post->ID , 'status' );
// Loop over each item since it's an array
if ( $terms != null ){
foreach( $terms as $term ) {
// Print the name method from $term which is an OBJECT
echo '<button class="filter "'.$term->slug.'"" data-filter="'.$term->slug.'">'".$term->name."'</button>';
// Get rid of the other data stored in the object, since it's not needed
unset($term);
}
}
?>
Rewrite your line as below:-
echo "<button class='filter {$term->slug}' data-filter='{$term->slug}'>$term->name</button>";
Please add below code and check.
<?php
$terms = get_the_terms( $post->ID , 'status' );
if ( $terms != null )
{
foreach( $terms as $term )
{
echo '<button class="filter "'.$term->slug.'"" data-filter="'.$term->slug.'">'".$term->name."'</button>';
unset($term);
}
}
?>
This seems like a simple enough problem but I'm a novice at PHP and I've been working on this for hours. I'm looping through posts in an archive and showing a different logo for each post based on a certain attribute. Here's my existing function in functions.php:
function show_logo() {
global $post;
$attribute_names = array( 'pa_product-type'
);
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
if ( $term->name == 'L1' ) {
// Show L1 Logo
}
elseif ( $term->name == 'M1' ) {
// Show M1 Logo
}
elseif ( $term->name == 'H1' ) {
// Show H1 Logo
}
else {
$full_line = '<span>'. $term->name . '</span>';
}
array_push( $terms_array, $full_line );
}
echo implode( $terms_array );
}
}
}
}
All I want to do is do show a different logo if the post matches multiple terms (e.g. 'L1' AND 'M1'). I have tried many very different things but I have no idea if I'm even on the right track. Any help would be greatly appreciated.
This should be fairly easy, I'm just not exactly sure the full context of all the data involved.
Assuming you are only showing one logo per post, here is one approach:
Right before foreach ( $terms as $term ) { create three boolean variables:
$hasL1 = false;
$hasM1 = false;
$hasH1 = false;
Then when one of your term names matches, instead of just showing the logo set the appropriate variable equal to true, ie $hasL1 = true;
After the foreach is complete, either before or after echo implode( $terms_array ); depending on what makes sense, setup a new if/elseif/else block to decide what logo to show as follows:
if ($hasL1 && $hasM1 && $hasH1) { // Pick logo for all 3 }
elseif ($hasL1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1 && $hasH1) { // Pick logo for pair }
elseif ($hasH1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1) { // Pick logo }
elseif ($hasM1) { // Pick logo }
elseif ($hasH1) { // Pick logo }
else { // default logo }
Of course there are many other ways to implement this too.