5

If user click on button in Product view page , we are redirecting to another url [ say 1.html] with help of js code : window.location.replace(responseData.redirect);. But i want to redirect only if user is Registered.

.js :

_ajaxCreateImg: function(dataUrl, startNumber) 
{ 
    new Ajax.Request(this.createImgUrl, { 
        onComplete: function(response) { 
            var responseData = response.responseText.evalJSON(); 
            if(lastRequest == 1){ 
                this._processAjaxResponse(response); 
                window.location.replace(responseData.redirect); 
            } 
         }.bind(this) 
     }); 
},
  • How do you include this JS file? In config.xml in template-file? – sv3n May 22 '17 at 12:01
  • @sv3n we have above code in Js/Abstract.js file , if user is logged in , we want to execute this code : window.location.replace(responseData.redirect); , otherwise not...... – user52996 May 22 '17 at 12:03
  • Question was how do you load Js/Abstract.js. Is it set in config.xml or in html code via <script ...? – sv3n May 22 '17 at 12:06
  • @sv3n we have in 2 places : 1)config file : <action method="addJs"><script>aitoc/aitcg/Aitcg/Editor/Tool/Abstract.js</script></action> 2)template file : <script type="text/javascript" src="<?php echo $this->getJsUrl('aitoc/aitcg/Aitcg/Editor/Tool/Abstract.js')?>"></script> – user52996 May 22 '17 at 12:13
6

I would set a new variable in template file, e.g.

<script type="text/javascript">
//<![CDATA[
    var isLoggedIn: <?php echo (int) Mage::getSingleton('customer/session')->isLoggedIn() ?>
    });
//]]>
</script>

And modify JS function like:

_ajaxCreateImg: function(dataUrl, startNumber) {
    new Ajax.Request(this.createImgUrl, {
        onComplete: function(response) {
            var responseData = response.responseText.evalJSON();
            if (lastRequest == 1) {
                this._processAjaxResponse(response);
                if (isLoggedIn == 1) { # added
                    window.location.replace(responseData.redirect);
                }
            }
        }.bind(this)
    });
},
  • Wouldn't it be better to pass it through via require JS? Writing JS inside a template file seems wrong and not best practice. – Ben Crook May 22 '17 at 14:01
  • @BenCrook Can you please post an example (or link to). I know there are different solutions like add an helper for addItem and check for logged in there, but then you have to split JS into serveral files to keep other funtionality for logged out users. – sv3n May 22 '17 at 15:04
  • 3
    I'm 100% wrong as this is a Magento 1 question, if it was Magento 2 this wouldn't of been best practice but for Magento 1 it's fine - I'm just an idiot :P – Ben Crook May 22 '17 at 15:10

Your Answer

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