0

I want to edit the Magento 1.9.4 CartController.php file, to change the redirect when a user is on a mobile device (max screen width 750px).

I set the default Magento add to cart redirect method to stay on the same page and not redirect to the cart. But when a user is on a mobile device, I want to redirect them direct to the cart.

How can I achieve that?

0

You cannot check device resolution in .php file, so instead of resolution you can check mobile agent.

To do this add a helper function to check if mobile device is requesting in any of your custom module helper app/code/Vendor/CustomModule/Helper/Data.php

public function isMobile(){
    //return true;
    $regex_match = "/(nokia|iphone|ipad|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|"
        . "htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|"
        . "blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|"
        . "symbian|smartphone|mmp|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|"
        . "jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220"
        . ")/i";

    //DISPLAY DESKTOP THEME ON HAUWEI TAB
    if(preg_match("/(huaweimediapad)/i", strtolower($_SERVER['HTTP_USER_AGENT']))){
        return false;
    }

    if (preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']))) {
        return TRUE;
    }

    if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
        return TRUE;
    }

    if(stripos($_SERVER['HTTP_USER_AGENT'],"Android") && stripos($_SERVER['HTTP_USER_AGENT'],"mobile")){

        return true;

    }

    if(stripos($_SERVER['HTTP_USER_AGENT'],"Android")){

        return false;

    }



    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
    $mobile_agents = array(
        'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
        'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
        'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
        'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
        'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
        'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
        'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
        'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
        'wapr','webc','winw','winw','xda ','xda-');

    if (in_array($mobile_ua,$mobile_agents)) {
        return TRUE;
    }

    if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
        return TRUE;
    }

    return FALSE;
}

before redirection you can check if it is mobile by using this code:

if(Mage::helper("CustomModule")->isMobile()){
    //Your mobile redirection code would be here
}
| improve this answer | |
  • Thanks! I am running a Magento 1.9.4 store, so how to use it in M1? – JGeer Apr 11 '19 at 13:11
  • This code is for M1 – Shoaib Munir Apr 11 '19 at 14:42

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.