Y
Y
Yuri Voronin2019-02-26 17:35:45
1C-Bitrix
Yuri Voronin, 2019-02-26 17:35:45

How to make friends composite and mobile version of the site?

Hello.
Composite was included on the site. The task was to make a mobile version. I created a new template, implemented a mob in it. version. In the site settings, I specified that the mobile template should be loaded using the PHP expression siteType=='pda' .
init.php

require_once($_SERVER["DOCUMENT_ROOT"]."/include/Mobile_Detect.php");
$detect = new Mobile_Detect();
$flag = $detect->isMobile();
if (strpos($APPLICATION->GetCurDir(), '/product/')!== false || strpos($APPLICATION->GetCurDir(), '/catalog/')!== false || $APPLICATION->GetCurPage(false) === '/') {
    if (!$detect->isMobile()) {
        setcookie('siteType', '', time()+3600*24*30,'/');
        define('siteType','');
        setcookie("MOBILE_VISITOR_MB", "", time()-1000);
    } else {
        if (isset ($_GET['type'])){
            switch ($_GET['type']) {
                case 'pda':
                    setcookie('siteType', 'pda', time()+3600*24*30,'/');
                    define('siteType','pda');
                    break;
                default:
                    setcookie('siteType', '', time()+3600*24*30,'/');
                    setcookie("MOBILE_VISITOR_MB", "", time()-1000);
                    define('siteType','original');
            }
        }
        else{
            $checkType='';
            if (isset($_COOKIE['siteType'])) $checkType=$_COOKIE['siteType'];
            switch ($checkType) {
                case 'pda':
                    define('siteType','pda');
                    break;
                default:
                    define('siteType','');
                    setcookie("MOBILE_VISITOR_MB", "", time()-1000);
            }
        }
    }
} else {
    define('siteType','');
    setcookie("MOBILE_VISITOR_MB", "", time()-1000);
}

header.php of the main template
require_once($_SERVER["DOCUMENT_ROOT"]."/include/Mobile_Detect.php");
    $detect = new Mobile_Detect();
    $flag = $detect->isMobile();
    if (strpos($APPLICATION->GetCurDir(), '/product/')!== false || strpos($APPLICATION->GetCurDir(), '/catalog/')!== false || $APPLICATION->GetCurPage(false) === '/') {
        $VISITOR_ID = $APPLICATION->get_cookie("MOBILE_VISITOR_MB");
        if (($detect->isMobile()) && empty($VISITOR_ID)) {
            $APPLICATION->set_cookie("MOBILE_VISITOR_MB", "MOBILE", time() + 60 * 60);
            LocalRedirect("https://".$_SERVER["SERVER_NAME"] . $_SERVER["REDIRECT_URL"] . "?type=pda");
            exit();
        }
    }

header.php of the mobile template
require_once($_SERVER["DOCUMENT_ROOT"]."/include/Mobile_Detect.php");
        $detect = new Mobile_Detect();
        $flag = $detect->isMobile();
        if (strpos($APPLICATION->GetCurDir(), '/product/')!== false || strpos($APPLICATION->GetCurDir(), '/catalog/')!== false || $APPLICATION->GetCurPage(false) === '/') {
            if (!$detect->isMobile()) {
                $APPLICATION->set_cookie("MOBILE_VISITOR_MB", "", time() - 60 * 60);
                LocalRedirect("https://".$_SERVER["SERVER_NAME"] . $_SERVER["REDIRECT_URL"] . "?type=original");
                exit();
            }
        }

When the composite is off, everything works correctly. Versions are correct. But once you turn on the composite, everything breaks down. The desktop version can be shown on the mobile phone, and the mobile version on the desktop. As I understand it, this is due to the fact that the composite takes data from the cache and it turns out that the check for the device type is ignored. The latest version of the page in the cache is taken. Is it possible to somehow use template substitution and composite?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Yuriev, 2019-02-27
@yurievyuri

Did you try to connect all the necessary scripts for this at the init.php level, before the start of the Bitrix layers?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question