Jump to content

Mobile / Tablet code detection


Rissinko

Recommended Posts

Hi, I know that in prestashop is implemented Mobile detect php class.. But I cant figure how can I detect if someone use tablet. I need use it in header.tpl file.

 

 

I use this in my classes/product.php

$ismobile = Context::getContext()->getMobileDevice(); 

But i think it is for all mobile devices. But i need one variable for tablet and one for mobile.

 

 

 

 

 

Link to comment
Share on other sites

  • 2 years later...

PrestaShop v1.6 has the $mobile_device variable available to TPL files:

{if $mobile_device}OK{else}NO{/if}

That won't work for tablet detection though. You could try the following for it:

{if Context::getContext()->isTablet()}OK{else}NO{/if}

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

In your php file you can write following and pass it to tpl.

{file_name}.php

    $this->context->smarty->assign(array(

        'is_mobile' => Context::getContext()->isMobile(),

        'device_type' => Context::getContext()->getDevice(), 

    ));

----------------------------------------------------

In your tpl file you can use it like this.

{file_name}.tpl

    {if isset($is_mobile) && $is_mobile == 1}

        <div class="for_mobile">MOBILE</div>

    {else}

        <div class="for_other_devices">---</div>

    {/if}

 

NOTE: getDevice() returns 1,2,4 for device types

    const DEVICE_COMPUTER = 1;

    const DEVICE_TABLET = 2;

    const DEVICE_MOBILE = 4;

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...