0

I am doing popup in magento2 and my code is like . When I click subscribe button page got refresh and again it appear . So I am in infinty loop . How in magento we make it for 1 time session on home page ?

<div id="custom-popup-modal">
    <div class="col-sm-6">
        <div class="static_content">
            <h5 class="min-title">JOIN THE PARTY</h5>
            <h2 class="min-title">GET 15% OFF YOUR FIRST ORDER*</h2>
            <p class="content">Subscribers only discounts, first look at new products, and cutting-edges tips</p>
            <?php echo $newsletterHtml; ?>
            <p class="micro-text">*Promo code will be emailed to the address you enter, New subscribers only</p>
        </div>
    </div>
    <div class="col-sm-6 img-section">
        <img src='<?php echo $this->getUrl('pub/media/').'cms/image-email.gif'; ?>' />
    </div>
</div>

<script type="text/javascript" xml="space">

    require(['jquery','Magento_Ui/js/modal/modal'],

        function($,modal) {

            var options = {
                type: 'popup',
                responsive: true,
            };

            var popup = modal(options, $('#custom-popup-modal'));
            $( document ).ready(function() {
                $('#custom-popup-modal').modal('openModal');
            });
        }
    );

</script> 
1

Try below code. You can set Cookie time accordingly.

<script type="text/javascript" xml="space">
    require([
        'jquery',
        'Magento_Ui/js/modal/modal',
        'mage/cookies'
    ],function($,modal) {
            if(!$.cookies.get("foo")){
                var date = new Date();
                var minutes = 60;
                date.setTime(date.getTime() + (minutes * 60 * 1000));
                $.cookie('foo', '', {path: '/', expires: -1}); // Expire Cookie
                $.cookie('foo', 'bar', {expires: date}); // Set Cookie Expiry Time
                $.cookie('foo', 'setvalue'); // Set Cookie Value
              }
                var options = {
                    type: 'popup',
                    responsive: true,
                };
                var popup = modal(options, $('#custom-popup-modal'));
                $( document ).ready(function() {
                    $('#custom-popup-modal').modal('openModal');
                });
            }
        }
    );
</script> 
  • is it working ?? – Abhishek Panchal Nov 23 '17 at 18:03
  • let me check please – Learner Nov 23 '17 at 19:44
  • where to set time ? in lifetime place ? – Learner Nov 23 '17 at 19:45
  • var cookie = $.mage.cookies['defaults']['lifetime']; here – Abhishek Panchal Nov 23 '17 at 19:46
  • 1
    if the same user come back to website some another day then you want to display? – Abhishek Panchal Nov 24 '17 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.