L
L
lagudal2019-05-16 11:00:22
JavaScript
lagudal, 2019-05-16 11:00:22

Why does popup work incorrectly in M2?

Hello, this is the problem.
We want to immediately greet visitors from the start with a modal window in which it is proposed to choose whether the client is an individual or a company. In order to immediately show the corresponding prices.
Those. a window pops up, select one of the two options in the drop-down list, press ok and that's it.
But here my colleagues are too smart, in my opinion, and I myself can’t find where the dog is buried - the problem is that after selecting the option in the dropdown, the window closes without waiting for ok - in general, let it be so. But after closing, the window opens again in a second, and so on until you forcibly press ok or x.
And after reloading the page, the window pops up again and again, even if you chose the first - default - option and clicked ok. There seems to be a problem with the storage of cookies, if I understand correctly.
The site is under development, unfortunately, it is only accessible from the internal network, so I can only provide the popup code.
Actually, here he is.

spoiler
<div id="custom-popup-modal">
<?php if ($block->getHelper()->usePrivateContent()):?>
  <div class="welcome"><?php echo __("We're happy that you've chosen us!")?></div>
  <div class="choose"><?php echo __("Would you like to continue as a business customer or private customer?")?></div>
  <!--  <li class="switcher-label" data-bind="scope: 'switcher'"><span data-bind="text: switcher().label"></span></li>-->	
  <li class="switcher-dropdown" data-bind="scope: 'switcher'">
    <script type="text/x-magento-init">
 		{
            "*": 
      {
                "Magento_Ui/js/core/app": 
        {
                    "components": 
          {
                        "switcher": 
            {
                            "component": "Anowave_TaxSwitch/js/switcher",
              "config":
              {
                "endpoint": "<?php echo $block->getUrl('taxswitch/index/index') ?>"
              }
                        }
                    }
                }
            }
        }
    </script>
    <script>
      var bindTrack  = <?php echo $block->getHelper()->getTrack() ?>; 
      var bindRender = function()
      {
        return true;
      };
      var bindRenderOptions = function()
      {
        <?php if (false):?>
        
          <?php
          /**
           * This is kind of dirty workaround. value: switcher().current doesn't seem to work in Magento 2.1.2
           */
          ?>
          var select = jQuery('select[name=tax_display]'), current = jQuery(':hidden[name=current]').val(), current_arg = jQuery(':hidden[name=current_arg]').val();
  
          if (0 < parseInt(current_arg))
          {
            select.val(current_arg);
          }
          else 
          {
            select.val(current);
          }

        <?php endif ?>

        return this;
      };
      var bindChange = function(target, event)
      {
         if (event.originalEvent)
         {
         	if (true == bindTrack.enable)
          {
            /**
             * Get tax display type
             */
            bindTrack.fieldsObject['eventLabel'] = jQuery('select[name=tax_display]').find('option:selected:first').text();

            if ('ua' == bindTrack.type) 
            {
              if ('undefined' !== typeof ga)
              {
                ga('send', bindTrack.fieldsObject);						
              }
            }
            else 
            {
              if ('undefined' !== typeof dataLayer)
              {
                bindTrack.fieldsObject['event'] = 'taxSwitch';

                /**
                 * Push event to dataLayer
                 */
                dataLayer.push(bindTrack.fieldsObject);
              }
            }
          }
            
           jQuery(event.target).parents('form:first').submit();
         }
      };
  
    </script>
    <div data-bind="text: switcher().select"></div>
    <input type="hidden" id="current" name="current" data-bind="value: switcher().current" />
    <input type="hidden" name="current_arg" value="<?php echo (int) @$_GET['tax_display'] ?>" />
    <form method="post" action="<?php echo $block->getUrl('taxswitch/index/switch')?>">
      <select id="switchoptions" name="tax_display" class="tax_display_select" data-bind="event: { change: bindChange }, afterRender: bindRender, optionsAfterRender: bindRenderOptions, options: switcher().options, optionsText: 'label', optionsValue: 'value', value: switcher().current"></select>
    </form>
  </li>
  <?php else: ?>
  <?php
  /**
   * This will be deprecated in next version
   */
  ?>
  <li><?php echo __('Show prices') ?></li>
  <li>
    <form method="post" action="<?php echo $block->getUrl('taxswitch/index/switch')?>">
      <input type="hidden" name="form_key" value="<?php echo $block->getFormKey() ?>" />
      <select name="tax_display" class="tax_display_select" onchange="this.form.submit()">
        <?php foreach($block->getOptions() as $option):?>
          <option value="<?php echo $option['value'] ?>" <?php if ($option['checked']):?>selected<?php endif ?>><?php echo $option['label'] ?></option>
        <?php endforeach?>
      </select>
    </form>
  </li>
<?php endif ?>
</div>

<script>
require(
    [
        'jquery',
        'Magento_Ui/js/modal/modal'
    ],
    function(
        $,
        modal
    ) {

    	
        var options = {
            type: 'popup',
            responsive: true,
            innerScroll: true,
            modalClass: 'custom-popup-modal',
            buttons: [{
                text: $.mage.__('Close'),
                class: '',
                click: function () {
                    this.closeModal();
                }
            }]
        };

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

In general, I will be grateful for the help, otherwise something stuck here.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question