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 ;)
Related
So I'm working with the sinlge-download.php page and I'm trying to check if the specific product is in a specific category.
Here is what I tried but I only get the ELSE result even if the download is a book.
if( in_category( 'Books' ) ) {
echo 'This product is a book';
} else {
echo 'This product is not a book';
}
According to EDD docs, the category is: download_category Easy Digital Download Docs
For this... use function has_term since in_category refers to WordPress post type posts and not for custom post types like the downloads.
if( has_term( 'Books', 'download_category' ) ) {
echo 'This product is a book';
} else {
echo 'This product is not a book';
}
You can use this
if( has_term( $term = '', $taxonomy = '', $post = null ) ) {
// do something
}
// $term = Category OR Taxonomy name $taxonomy = Taxonomy Name. OR
// "category" if its default WP category $post = Post ID to check. Leave
// empty to pull this from global query
https://developer.wordpress.org/reference/functions/has_term/
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 am using the following to list the terms within my custom taxonomy
<?php $application_terms = wp_get_object_terms($post->ID, 'application');
if(!empty($application_terms)){
if(!is_wp_error( $application_terms )){
foreach($application_terms as $application){
echo $application->slug;
}
}
}
?>
The code works fine and it displays the terms however, if a post type has two "application" terms assigned to it, the two terms appear without space in betweeen them e.g ig apple and banana it appears applebanana. How do you change the code so that there is space in between the terms?
Thanks
Create an array of slugs and implode it with a blank space
$application_terms = wp_get_object_terms($post->ID, 'application');
if ( $application_terms && !is_wp_error( $application_terms ) {
$slugs = wp_list_pluck( $application_terms, 'slug' );
$string = implode( ' ', $slugs );
echo $string;
}
It should work in the way, you want it to be
<?php $application_terms = wp_get_object_terms($post->ID, 'application');
if(!empty($application_terms)){
if(!is_wp_error( $application_terms )){
$i = 0;
foreach($application_terms as $application){
if($i>0)
echo ', ';
echo $application->slug;
$i++;
}
}
}
?>
so if a post have two category assigned, it will display like apple, banana
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">';
}
Okay, I'm trying to show certain tab content depending on what category a product is listed in.
I have created a custom "Size Chart" tab and want to show for example:
Footwear size chart when product is in "Footwear" category
Clothing size chart when product is in "Clothing" category
No size chart when product is in "Accessories" category.
I've created a custom tab like this:
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['new_tab'] = array(
'title' => __( 'Size Chart', 'woocommerce' ),
'priority' => 55,
'callback' => 'woo_tab_content'
);
return $tabs;
}
Then added the custom tab content like this:
function woo_tab_content() {
echo 'This is the size chart';
}
Does anyone know how to add a conditional statement to this code to allow me to specify what content is displayed depending on what category a product is placed in i.e.
is_product_category( 'footwear' )
Thanks!
in you woo_tab_content function simple add what you have done above so it would like this this:
function woo_tab_content() {
if (is_product_category( 'footwear' )){
echo 'This is the size chart';
} else {
//do something else
}
}
Okay, I managed to fix it using the following code:
function woo_tab_content() {
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'clothing', $categories ) ) {
echo 'this is the clothing tab content!';
} elseif ( in_array( 'shoes', $categories ) ) {
echo 'this is the shoes tab content!';
} else {
echo 'all other tabs';
}
}
It turns out that is_product_category does not work for the single product pages. Only for the category page itself.