PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] How do I echo different content for each product tag on a category page in WooCommerce?

 March 18, 2022     php, woocommerce, wordpress, wordpress-theming     No comments   

Issue

I am using URL parameters to filter my categories like so: https://example.com/collections/trousers/?product_tag=male

I want to echo different HTML depending on the tag used. Here's my code which should be pretty self explanatory:

add_action('bs_after_primary', 'custom_product_category_descriptions');

function custom_product_category_descriptions() {

if ( is_product_category( 'trousers' ) && ! is_product_tag( array( 'male', 'female' ) ) ) {
echo '<h1>Checkout these trousers for men and women</h1>';
  } elseif ( is_product_category( 'trousers' ) && is_product_tag ( 'male' ) && ! is_product_tag ( 'female' ) ) {
echo '<h1>Checkout out these trousers for Men</h1>';
  }
    elseif ( is_product_category( 'trousers' ) && ! is_product_tag ( 'male' ) && is_product_tag ( 'female' ) ) {
echo '<h1>Checkout out these trousers for Women</h1>';
  }
}

The above code echo's <h1>Checkout these trousers for men and women</h1> on all trouser category pages:

https://example.com/collections/trousers/

https://example.com/collections/trousers/?product_tag=male

https://example.com/collections/trousers/?product_tag=female

Why does the code echo on all URL's above ignoring my ! is not conditional statements?


Solution

Woo's is_product_tag() function detects whether you are viewing a product tag page. But you are viewing a product category page, not a tag page, so is_product_tag( anything ) always returns false.



Answered By - O. Jones
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing