ALOUI MH
ALOUI MH

Reputation: 97

Display a Popup in woocommerce shop page only

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

Answers (1)

Raunak Gupta
Raunak Gupta

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 in wp_head or wp_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

Related Questions