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?

1 Answer 1


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";

    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;



        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',
        '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:

    //Your mobile redirection code would be here
  • Thanks! I am running a Magento 1.9.4 store, so how to use it in M1?
    – JGeer
    Commented Apr 11, 2019 at 13:11
  • This code is for M1 Commented Apr 11, 2019 at 14:42

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.