Replace age restricted product content on shop / archive page
-
How can we replace age restricted product content on shop/archive page with the restricted content message and button?
-
I also search for similar solution – to add Age gate code\trigger to specific archive template (not woocommerce).
echo do(“shortcode”) is not working and cause fatal error.Hi @ashleycyber,
Could you give a bit more detail, not entirely sure what you need, sorry.
@ksahipuk – again, not sure what you’re trying to do here, do you mean
do_shortcode()
?Thanks
PhilHi Phill!
I have some custom post types and archive templates for this post types. So now I can see Age gate only on custom post types, but I need Age gate working on archive page. I have archive-page-template.php so what I need to add the for Age gate working on it?
(I google solution for adding shortcode to template but it’s not working)
I have a product category that is restricted – that works well on the product pages themselves and the category page, but not on the shop/archive page. Those products still appear, which is fine, but the ‘are you 21?’ should appear (like an overlay) instead of the product details (title, image, etc.) on those products, until the user confirms that they are, in fact, 21.
At the very least, those products shouldn’t show on the shop pages —- but that is not ideal as the user may be over the age and not realize that they aren’t seeing everything.
-
This reply was modified 4 years ago by
ashleycyber.
Phil,
in my case work this code after footer in template page<script type="text/javascript"> function conditionallyAllow(restrict, meta){ return window.location.pathname.match(/shop/); } if(typeof ageGateHooks !== 'undefined'){ ageGateHooks.addFilter('age_gate_restricted', 'age-gate', conditionallyAllow); } </script>
But it totally ignores plugins settings and triggers every time even for logged administrator
-
This reply was modified 4 years ago by
ksahipuk.
Phil – any ideas/thoughts on this?
Hi @ashleycyber,
Yes, sorry been a bit full on. There’s various ways to do this so kind of depends what you want to show the user. But here’s an example using the age gate shortcode.
I’m assuming you have WooCommerce here!
First, if you don’t have one, create a file:
yourtheme/woocommerce/content-product.php
If you do have one, you might need to adjust the below if anything isn’t default.
Most of this code is standard WooCommerce, but we tweak it slightly to our needs:
<?php /** * The template for displaying product content within loops * * This template can be overridden by copying it to yourtheme/woocommerce/content-product.php. * * HOWEVER, on occasion WooCommerce will need to update template files and you * (the theme developer) will need to copy the new files to your theme to * maintain compatibility. We try to do this as little as possible, but it does * happen. When this occurs the version of the template file will be bumped and * the readme will list any important changes. * * @see https://docs.woocommerce.com/document/template-structure/ * @package WooCommerce\Templates * @version 3.6.0 */ defined('ABSPATH') || exit; global $product; // Ensure visibility. if (empty($product) || ! $product->is_visible()) { return; } ?> <li <?php wc_product_class('', $product); ?>> <?php // Start output buffering so we can use the content later ob_start(); /** * Hook: woocommerce_before_shop_loop_item. * * @hooked woocommerce_template_loop_product_link_open - 10 */ do_action('woocommerce_before_shop_loop_item'); /** * Hook: woocommerce_before_shop_loop_item_title. * * @hooked woocommerce_show_product_loop_sale_flash - 10 * @hooked woocommerce_template_loop_product_thumbnail - 10 */ do_action('woocommerce_before_shop_loop_item_title'); /** * Hook: woocommerce_shop_loop_item_title. * * @hooked woocommerce_template_loop_product_title - 10 */ do_action('woocommerce_shop_loop_item_title'); /** * Hook: woocommerce_after_shop_loop_item_title. * * @hooked woocommerce_template_loop_rating - 5 * @hooked woocommerce_template_loop_price - 10 */ do_action('woocommerce_after_shop_loop_item_title'); /** * Hook: woocommerce_after_shop_loop_item. * * @hooked woocommerce_template_loop_product_link_close - 5 * @hooked woocommerce_template_loop_add_to_cart - 10 */ do_action('woocommerce_after_shop_loop_item'); // Get the Woocommerce generated content from the buffer $content = ob_get_clean(); // Grab a couple of settings $restrict = get_post_meta($product->get_id(), '_age_gate-restrict', true); // if everything is aged 21, you can just set this to // $age = 21 // or hard code it in the shortcode $age = get_post_meta($product->get_id(), '_age_gate-age', true) ?: 21; if ($restrict) { // see if the product is restricted, wrap it in the age gate shortcode echo do_shortcode('[age-gate age="' . $age . '" type="buttons" title="Restricted product."]' . $content . '[/age-gate]'); } else { // otherwise, just output the content echo $content; } ?> </li>
Restricted products will not have their own buttons and if you pass one, they should all show.
There’s other ways you could do it too of course, but this seems the most logical based on the thread.
Thanks
Phil@ksahipuk – You’re on the right lines, but you’re telling age gate to always show on the shop no matter what. Something like this should help:
<script type="text/javascript"> function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function conditionallyAllow(restrict, meta) { var cookie = readCookie('age_gate'); return window.location.pathname.match(/shop/) && !cookie; } if (typeof ageGateHooks !== 'undefined') { ageGateHooks.addFilter('age_gate_restricted', 'age-gate', conditionallyAllow); } </script>
The
meta
parameter should have some more info you can test against if you need to. You can log it out to the console to see what’s in there.If there’s anything else you need I’d suggest opening a new thread as this will be marked resolved once @ashleycyber’s original question is shorted.
Cheers
PhilPhil – this works great if the individual products are manually marked as restricted – thank you! What do I need to change to hide any/all products in a product category that is restricted?
I changed one line of your code from
if ($restrict) {
to:
if ($restrict or has_term( ‘gifts-rewards’, ‘product_cat’ )) {
———————————————————————-
Is there a better way?
Hi @ashleycyber – if there’s only going to be ‘gift-rewards’ that need restricting, I think this is probably the best way. If there’s potentially multiple then you could do
get_the_terms($product->get_id())
and loop over them and useget_term_meta()
.Terms use the same meta keys as posts, so
get_term_meta($id, '_age_gate-restrict', true)
.Like I say, if it’s just the one, I think you’re set.
Thanks
PhilThank you!
So now if a user goes to a restricted category page, and clicks the yes, I’m over 21 button, the individual products beneath still have the restricted item notice, unless the user refreshes the page. I solved this by disabling “Use JavaScript Age Gate”, but isn’t as nice regarding transitions that way. Is there a way to keep JS but not have the individual items showing the notice if a user passes the test on a category page?
Hi @ashleycyber,
I’m not sure what you mean here? When you pass the shortcode it should show all the products applicable to that age.
Have you got a link I can see?
Ta
PhilIt happened once, but now I cannot recreate the issue now – so I think we’re good. Thank you so much for your help.
Now we noticed that on a shop page that is restricted, and you click Yes, I’m 21+, the notice goes away as expected, but if the user then goes to the next page the notice appears again. Any ideas? (https://staging.yougrogurl.com/shop/gifts-rewards as an example)
-
This reply was modified 4 years ago by
- The topic ‘Replace age restricted product content on shop / archive page’ is closed to new replies.