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? 🧐
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.