Hi @dannyfoo,
There’s a couple of ways you can do this, so depends on what works best for you.
If you’re not using the JavaScript version of Age Gate, you can filter the terribly named age_gate_unrestricted_unrestricted:
add_filter('age_gate_unrestricted_unrestricted', function ($restricted, $meta) {
if (!is_archive() && get_post_type() === 'product') {
return $_COOKIE['age_gate'] ?? 0 < $meta->age;
}
return $restricted;
}, 10, 2);
Above we check its not an archive, and is a product and then check for the cookie. Job done.
If you are in JS mode, it’s slightly different:
add_filter('age_gate_restricted', function ($restricted, $meta) {
if (!is_single($_POST['id']) && get_post_type($_POST['id']) === 'product') {
return $_COOKIE['age_gate'] ?? 0 < $meta->age;
}
return $restricted;
}, 10, 2);
Similar stuff, but uses some post variables as it does and AJAX call to apply the filter. Because it’s an Ajax call, there could be a bit of lag between the page loading and Age Gate showing.
A third option that would work for both is to automatically assign it on save (I’ve not tested this one!):
add_action('save_post_product', function($id) {
update_post_meta( $id, '_age_gate-restrict', 1);
});
This will automatically restrict anything on save, but means you’d never be able to not restrict a product. Any existing products you could write a quick script to restrict by adding the post meta.
Lastly there’s the backwards approach that doesn’t need any code. If more things are restricted than not, you can set it to restrict everything, and then bypass things individually. Essentially the opposite of how you have it set up at the moment.
Hope that’s helpful.
Cheers
Phil