Show Text for Certain Category Only
-
I want to set some conditions for the product page so that if the category is SOCKS (for example) certain text will appear. Otherwise, different text will appear. I tried adding this to variable.php, but it’s displaying an error:
<?php if ( is_product_category( array( ‘socks’ ) ) ) {
<p>show me</p>
}else {
<p> show this</p>
?>
Any help is appreciated!
-
You need to turn off php when using html. Sorry not tested.
<?php if ( is_product_category( array( ‘socks’ ) ) ) { ?> <p>show me</p> <?php } else { ?> <p> show this</p> <?php } ?>
Did you try adding your text at:
Dashboard > WooCommerce > Products > Categories > Socks > Edit > Description field.
Shows on the category page for me, but as the note says, its theme dependant.Thank you for replying! That’s showing the ‘show this’ text now, but not switching it out if the product category is ‘socks’. Am thinking that first part may be wrong.
I can do it under there but I need the text to show under the ‘Add to Cart’ button and can’t get it in the right place.
Infact changing it in the description page doesn’t add it to individual products. This is for products rather than the category page.
Products page – got it!
is_product_category() will always be false on a product page because a product page is not a category page. This function is only useful on a category page.
Your starting point in your function is:
if (is_product()) {
to test whether your code is running on a product page.A product can be in none, one or several categories, so you need to get all its categories:
global $post;
$terms = get_the_terms( $post->ID, ‘product_cat’ );
and then test each one:
foreach ($terms as $term) {
if ($product_cat_id == $term->term_id) {
where $product_cat_id is the id of the category which you are looking forTo get your output in the right place, have a look at:
wp-content/plugins/woocommerce/templates/content-single-product.php
unless your theme has a template override, in which case look at
wp-content/themes/my-theme-name/woocommerce/content-single-product.phpadd-to-cart is at priority 30 normally, so if you want to go in just after that you would use do_action( ‘woocommerce_single_product_summary’, ‘my-function’, 35 );
Thanks! So are you saying my code should be:
<?php if (is_product()) {
global $post;
$terms = get_the_terms( $post->ID, ‘socks’ );foreach ($terms as $term) {
if ($socks == $term->term_id) { ?><p>show me</p>
<?php
} else {
?>
<p> show this</p>
<?php
}
?>That’s giving me an error. I have this in variable.php (through my theme). It is showing in the correct place in here, though.
-
This reply was modified 8 years, 4 months ago by
ivcatalina.
This useful service:
https://phpcodechecker.com/
checks syntax and that brackets are in pairs.<?php if ( is_product() ) { global $post; $socks = 99; // category id for socks $terms = get_the_terms( $post->ID, 'product_cat' ); // no socks here foreach ($terms as $term) { if ($socks == $term->term_id) { print '<p>show me</p>'; } else { print '<p> show this</p>'; } } }
This is correct php but I’ve not tested that it does what you want.
Thank you! It’s close, i’m sure, but on the frontend it shows this:
show this
show this
show this
show this
show this
.And on the pages of the ‘socks’ category it shows this:
show this
show this
show this
show this
show me
So I just need to work out how to get rid of all the ones before it :). I wonder if it’s because there are multiple categories associated with it? So when the product sits inside ‘socks’ and ‘shoes’ for example, it’s showing ‘show this’ (for shoes) and ‘show me’ (for socks) as that’s what applies. Is there a way around this, do you know?
-
This reply was modified 8 years, 4 months ago by
ivcatalina.
-
This reply was modified 8 years, 4 months ago by
ivcatalina.
-
This reply was modified 8 years, 4 months ago by
ivcatalina.
Did you put it inside functions.php and call it by the hook?
add_action( ‘woocommerce_single_product_summary’, ‘my-function’, 35 ); function my-function() { // as above }
-
This reply was modified 8 years, 4 months ago by
lorro. Reason: code edit
This gives me an error (invalid function name). This is my code:
add_action( ‘woocommerce_single_product_summary’, ‘cat_condition’, 35);
if ( is_product() ) {
global $post;
$socks = 116; // category id for socks
$terms = get_the_terms( $post->ID, ‘product_cat’ ); // no socks here
foreach ($terms as $term) {
if ($socks == $term->term_id) {
print ‘<p>show me function</p>’;
} else {
print ‘<p> show this function</p>’;
}
}
}And thank you again for helping me with this!!
Yes you need a function name line.
You can’t put the prints inside the foreach $terms loop because its possible for a product to have multiple categories, and if so you would get both the positive for the required category, and negatives for the others. So we need to rewrite the code to first detect if the required category is included, and a second part to do the required print.
add_action( 'woocommerce_single_product_summary', 'cat_condition', 35); function cat_condition() { global $post; if ( is_product() ) { $socks = 116; // category id for socks $terms = get_the_terms( $post->ID, 'product_cat' ); $found = false; foreach ($terms as $term) { if ($socks == $term->term_id) { $found = true; break; } } // end for if ($found) { print '<p>Socks found!</p>'; } else { print '<p>No socks here!</p>'; } } // end if } // end function
This worked, THANK YOU!! Last question, if we wanted to change it to say be socks and accessories to be display 1 text, everything else to display something else, how would we add on ‘accessories’ to this condition?
Thanks again!!
|| means “or”:
if ( $socks_cat_id == $term->term_id || $accessories_cat_id == $term=>term_id ) {
THANK YOU!! You are a genius!
-
This reply was modified 8 years, 4 months ago by
- The topic ‘Show Text for Certain Category Only’ is closed to new replies.