I have a code to display checkbox on my single product pages in my WooCommerce shop. I want to only execute it for 2 product categories, unfortunately when I try to merge my code with conditional logic, I get a syntax error:
Parse error: syntax error, unexpected '?>' in [...]/functions.php on line 257
This is my code:
add_action( 'woocommerce_after_add_to_cart_quantity', 'checkbox-eco-packaging', 35 );
function checkbox-eco-packaging() {
if( has_term( array( 'sneakers', 'backpacks' ), 'product_cat' ) {
// do something if product is either in category "sneakers" or "backpacks"
?>
<label><input type="checkbox" name="eco-pack" value="Yes">♻️ Eco packaging?</label>
<?php
}
/**
else {
// do something else if it isn't
}
**/
}
And it does work (but on all product pages) without the conditional logic:
add_action( 'woocommerce_after_add_to_cart_quantity', 'checkbox-eco-packaging', 35 );
function checkbox-eco-packaging() {
?>
<label><input type="checkbox" name="eco-pack" value="Yes">♻️ Eco packaging?</label>
<?php
}
How to use the php tag with conditional logic correctly in this case? Thank you in advance for any tips.