Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exclude specific product from a function

I’m using a php function to add a small text after the ADD TO CART button in woocommerce for all the products. Works fine, but I need to exclude certain products from this.

Right now i’m using:

function woo_show_some_text_2() {
    echo '<p>customtext</p>';}

add_action( 'woocommerce_after_add_to_cart_button', 'woo_show_some_text_2', 20 );

How could I add a part in order to exclude specific products by ID for example? 🧐

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you in advance.

Can’t find a solution for this specifically.

>Solution :

To exclude specific product IDs from your function, you can use the following:

function woo_show_some_text_2() {
    global $product;

    // HERE define the product IDs to be excluded
    $excluded_product_ids = array( 16, 18, 25 );

    if ( in_array( $product->get_id(), $excluded_product_ids ) ) {
        return;
    }

    echo '<p>customtext</p>';
}
add_action( 'woocommerce_after_add_to_cart_button', 'woo_show_some_text_2', 20 );

Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading