Support » Plugin: Age Gate » Hide age restricted products on shop overview?

  • Resolved wzshop

    (@wzshop)


    Hi,
    Is it possible to filter out age restricted products from the shop overview page? And show them only when someone has verified their age?
    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Phil

    (@philsbury)

    Hi @wzshop,

    It’s not part of the plugin as I haven’t found a sensible use case for it – here’s the scenarios I’ve thought of

    1. If a site has all their content restricted, then someone will pass the age gate on entry and the content will show, making hiding it pointless.
    2. A site has selected content and the posts would be filtered out using pre_get_posts – how would someone pass an Age Check if they cannot visit the posts – basically I think they’re never be discoverable

    I’d also have potential concerns about caching here as that could play havoc with what people can and can’t see.
    If you have a thought on how this should work do let me know 🙂

    You could implement a version though with pre_get_posts and checking for the Age Gate meta items.

    Thanks
    Phil

    You can always implant a version yourself w

    Thread Starter wzshop

    (@wzshop)

    Hi Phil,
    Wow, thanks for your extensive answer and thoughts.
    To get back to you on point 2; this is not what I experience.
    So I have selected content that is age restricted. However this content still shows up in the product loop of the shop overview page and people can also order directly from that page (without going to the product page). Thus this way people can order age restricted products, without verifying their age (maybe an age check on the order page would do?). When going to the product page, however, the age verification popup shows.

    Thanks for pointing me the way to filter this out myself with the pre_get_posts hook. I will look into this further. Any further comments/thoughts will be greatly appreciated.
    Thanks!

    Plugin Author Phil

    (@philsbury)

    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

    Thread Starter wzshop

    (@wzshop)

    Hi Phil,
    Thanks a lot! I will use your code to customzie the add to cart button.
    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.