Texture Heaven
Texture Heaven

Reputation: 15

How to exclude Product Category ID from Shortcode Hook inside functions.php - WooCommerce

I am trying to exclude/disable a shortcode that I've added to the Single Product Template of woocommerce for specific product categories.

The code in functions.php is this:

add_action( 'woocommerce_single_product_summary', 'sizeguidemen', 20 );
 
function sizeguidemen() {
    echo do_shortcode('[elementor-template id="66083"]');
}

How can we exclude/disable the shortcode for specific product categories?

I believe it should be something with array( 'product_cat', '65' );?

I'm new to php and trying to do it, but doesn't work. :/

Upvotes: 1

Views: 292

Answers (1)

businessbloomer
businessbloomer

Reputation: 1132

Conditional logic allows you to run your functions only when certain criteria is met.

In your case, you're looking at the single product page and need to run a function only when category is 'whatever ID'.

By looking at WooCommerce Conditional Logic – Tags, Examples & PHP tutorial, and specifically at the "PHP: do something if product belongs to a category" section, your code would become the following, where "123" is the category ID you want that to work for:

add_action( 'woocommerce_single_product_summary', 'sizeguidemen', 20 );
 
function sizeguidemen() {
    if ( has_term( 123, 'product_cat' ) ) {
        echo do_shortcode('[elementor-template id="66083"]');
    }
}

You can even use category "slug" instead of "123", and also an array of these e.g. [123, 456, 789] or ['tables', 'chairs']

Upvotes: 2

Related Questions