Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento Redirect to Mobile theme programmatically


In my previous post i described how to redirect mobile theme in mobile browser.  Here you can see how it's work. But some time if you have created different cms page for different store it is not working in that redirection. In that redirection it will always call main store cms page.

If you getting such kind of problem you can now solve using my following solution. There is only few lines of code. Follow the step for redirect your main theme to mobile theme in mobile browser.

Step1: Add following code in your head.phtml file of your main theme(not in mobile theme).

<?php if($_SESSION['store'] =='') { ?>
 <script type="text/javascript">
 if (screen.width <= 720) {
      loadXMLDoc();
}
 function loadXMLDoc()
{
      var xmlhttp;
      if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
      }
       else
       {// code for IE6, IE5
             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
  xmlhttp.onreadystatechange=function()
 {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
           location.reload(true);
       }
   }
  xmlhttp.open("GET","http://www.yourdomain.com/mobileajax.php",true);
 xmlhttp.send();
}
</script>
<?php }?>

Step2: Create one mobileajax.php file in your root and add following code in it.

<?php
     session_start();
     $_SESSION['store'] ='mobile';
?>


Step3: Now add following code in your index.php file after 'umask(0);' this line of code.

session_start();
if(isset($_SESSION['store']))
{
    if($_SESSION['store'] =='mobile'){
        $_GET['___store'] ='mobile' ;
    }
}else{
    $_SESSION['store']="";

}


Now, clear your cache in test in your mobile and desktop browser.


Please support us, Like us on Facebook.

  1. It looks like this will load the mobile "store view" not the mobile theme. Is there a way to make it load the mobile theme? And I'm trying to make this work in Magento 1.9.

    ReplyDelete

 

Copyright @ 2017 HKBlog.