Reputation: 97
I am trying to set a popup for newsletter subscription in WooCommerce Shop Page Only with the use of a Cookie.
The problem: Setting a cookie in Wordpress should be done in functions.php in which Conditional Tags won't run as mentioned here: https://docs.woocommerce.com/document/conditional-tags/
Any suggestion?
Thanks
Upvotes: 1
Views: 1770
Reputation: 10809
If you want to show popup or want to add any script to shop page only then you have to use
is_shop()
conditional tag inwp_head
orwp_footer
action.
Try this code
function subscription_footer()
{
//for shop page only
if (is_shop())
{
//if cookie does not exist/set then perform your stuff.
if (!isset($_COOKIE['shop_subscribe']))
{
setcookie('shop_subscribe', 'yes', time() + (86400 * 30)); // 86400 = 1 day
$_COOKIE['shop_subscribe'] = 'yes';
//here you can write your html/js code for popup.
}
}
//print_r($_COOKIE);
}
add_action('wp_footer', 'subscription_footer');
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Upvotes: 2