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

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source