3

I need to show a popup when the store loads and need to ask users to select the city, then only products deliverable to these cities must show.

In the backend, I added some cities to a new attribute 'location'. So in the popup, I need to, maybe query those data in attribute and show them.

Previously I made modules for separate pages. Which has a route id and all? But here I need it on the main page.

Which template, layout should I extend? And all.

2 Answers 2

3

I used magnific popup you can download source from Link

VendorName: Prince

ModuleName: Popup

You can see my full Popup module Here: Github

app/code/Prince/Popup/view/frontend/layout/cms_index_index.xml

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <link src="Prince_Popup/js/jquery.magnific-popup.min.js"/>
        <css src="Prince_Popup/css/magnific-popup.css"/>   
        <css src="Prince_Popup/css/magnific-animation.css"/>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="index.home" template="Prince_Popup::index/popup.phtml"/>
        </referenceContainer>
    </body>
</page>

app/code/Prince/Popup/view/frontend/requirejs-config.js

var config = {
    map: {
        '*': {
            magnificPopup: 'Prince_Popup/js/jquery.magnific-popup.min',       
            popup: 'Prince_Popup/js/prince_popup'
        }
    },
    shim: {
        magnificPopup: {
            deps: ['jquery']
        }
    }
};

app/code/Prince/Popup/view/frontend/templates/index/popup.phtml

  <div id="popup-content" class="popup-content mfp-with-anim" style="display:none">
      YOUR CONTENT HERE
  </div>

  <script type="text/javascript">
    requirejs(['jquery', 'popup' ],
      function ($, popup) {
          $(document).ready(function(){
              $.magnificPopup.open({
                items: {
                  src: '#popup-content'
                  },
                  type: 'inline',
                  removalDelay: 500
                });
              $('#popup-content').css('display','inline block');
          });
      });
  </script>
12
  • Getting an error at require.js:166 uncaught error mismatched anonymous define module
    – Mann
    Commented May 12, 2017 at 11:15
  • @Mann can you share your requirejs-config.js Commented May 12, 2017 at 11:17
  • I have pasted what you have given above
    – Mann
    Commented May 12, 2017 at 11:19
  • can you do static content deploy. and you can see my full module here: github.com/prince108/Magento2-PopupManager Commented May 12, 2017 at 11:31
  • Yea I have done it. But I also mentioned another thing, that is I want to load all the cities the store can deliver in that popup. I am trying to to get all the attribute values. I created an attribute 'location' to all the products uploaded by admin. Reference site for what I want to achieve, inox movies
    – Mann
    Commented May 12, 2017 at 13:28
2

My upper answer is for magnify popup but if you want default magento 2 modal popup on homepage you need to create this files

app/design/frontend/Vendor/theme/Magento_Cms/layout/cms_index_index.xml

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <link src="Magento_Cms/js/popup.js"/>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\template" name="index.home" template="Magento_Cms::popup.phtml"/>
        </referenceContainer>
    </body>
</page>

app/design/frontend/Vendor/theme/Magento_Cms/templates/popup.phtml

<div id="popup-content" class="popup-content" style="display:none">
    YOUR CONTENT HERE
</div>

app/design/frontend/Vendor/theme/Magento_Cms/web/js/popup.js

require(
    [ 'jquery', 'Magento_Ui/js/modal/modal' ],
    function($, modal) {
        modal({
            autoOpen: true,
            responsive: true,
            clickableOverlay: false,
            modalClass: 'modal-custom',
            title: 'Popup'
        }, $("#popup-content"));
    }
);
4
  • Why is the JS in a template file? Wouldn't it be better practice to have JS in a JS file? Markup and logic should be separated IMO.
    – Ben Crook
    Commented May 18, 2017 at 16:20
  • 1
    @Ben Crook yes you are right but this is just example to explain in short so new developer can understand easily Commented May 19, 2017 at 3:18
  • I thought so, but wouldn't it be better to teach new developer how to do it with best practice? Otherwise they'll be learning techniques that aren't good practice
    – Ben Crook
    Commented May 19, 2017 at 8:36
  • 1
    @Ben Crook Thank you for your suggestion I updated my answer with different js and phtml file Commented May 19, 2017 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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