Hi @wzshop,
That’s a really good point, you get the add to cart for non-variable products.
In that case, I’d unhook the add to cart button and replace it with your own, something like (assuming you’re using woocommerce here!):
// remove the action - might need to wrap this in an init action
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
add_action('woocommerce_after_shop_loop_item', function(){
global $product;
$restricted = get_post_meta($product->get_id(), '_age_gate-restricted', true);
$age = get_post_meta($product->get_id(), '_age_gate-age', true) ?: 21;
if ($restricted && $_COOKIE['age_gate'] ?? 0 < $age) {
// not age check, send them to the product page where
// they will be age gated
echo sprint('<a href="%s">View product</a>'), get_permalink());
} else {
// do the standard WC function if it doesn't meet our test
//
woocommerce_template_loop_add_to_cart();
}
}, 10);
That’s all off the top of my head so isn’t tested, but should be a starting point at least! (also, excuse any bugs)
Thanks
Phil