EC-CUBE3でPC、スマホ、タブレットの判定方法

EC-CUBE3.0.14からPCとスマホを判別するMobile Detectというライブラリが導入されています。

mobiledetect.net

使い方は、PHP側からはShoppingService.php

// device type
if ($this->app['mobile_detect']->isMobile()) {
    $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_SP);
} else {
    $DeviceType = $this->app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_PC);
}

とありますので参考になりますが、Twigから使う場合、以下の通りとなります。

{% if app['mobile_detect'].isMobile %}
    Mobile、Tablet
{% else %}
    PC
{% endif %}
{% if app['mobile_detect'].isTablet %}
    Tablet
{% else %}
    PC、スマホ
{% endif %}
{% if app['mobile_detect'].isMobile and app['mobile_detect'].isTablet == false %}
    スマホ
{% else %}
    PC、Tablet
{% endif %}

レスポンシブデザインだと余り使いどころは無いかもしれませんがTwigでPCとスマホを判断したいという時に参考にしてください。

EC-CUBE3.0.13以下の方は独自でMoible Detectライブラリを導入する必要があります。
導入方法は以下のプルリクエストを参考にしてください。

モバイル種別判断用ライブラリの導入 · EC-CUBE/ec-cube@f0f5c70 · GitHub

composer.jsonApplication.phpにそれぞれ定義を追加し、EC-CUBEのルートディレクトリよりcomposer installを実行すればインストールされます。