Оптимизированный вариант. За основу взят сниппет отсюда. По сути отличается тем, что сниппетом мы выводим только id страниц, а далее обрабатываем их с помощью Ditto. Таким образом решается проблема добавления изображений и цен к последним просмотренным товарам. А также исправлены ошибки добавления товаров в куки (функция array_shift заменена на функцию array_pop).
Создаем сниппет LastView:
<?php
$mode = isset($mode) ? $mode : 'show';
$outerTpl = isset($outerTpl) ? $outerTpl : '@CODE:[+rows+]';
$outerClass = isset($outerClass) ? $outerClass : 'last-view';
$rowTpl = isset($rowTpl) ? $rowTpl : '@CODE:[+id+],';
$maxRows = isset($maxRows) ? $maxRows : 5;
if(!function_exists('fetchTpl')) {
function fetchTpl($tpl){
global $modx;
$template = "";
if(substr($tpl, 0, 6) == "@FILE:") {
$tpl_file = MODX_BASE_PATH . substr($tpl, 6);
$template = file_get_contents($tpl_file);
} else if (substr($tpl, 0, 6) == "@CODE:") {
$template = substr($tpl, 6);
} else if($modx->getChunk($tpl) != "") {
$template = $modx->getChunk($tpl);
} else {
$template = false;
}
return $template;
}
}
if(!function_exists('parseTplChunk')) {
function parseTplChunk($tpl,$placeholder=array()){
$chunk = fetchTpl($tpl);
foreach ($placeholder as $key => $value){
$chunk = str_replace("[+".$key."+]", $value, $chunk);
}
return $chunk;
}
}
$item = array();
if (isset($_COOKIE['last_view']) and $_COOKIE['last_view'] != '') {
$item = explode(',', $_COOKIE['last_view']);
}
switch ($mode) {
case 'register':
if (!in_array($modx->documentIdentifier, $item)) {
if (count($item) >= $maxRows) {
array_pop($item);
array_unshift($item, ($modx->documentIdentifier + ""));
} else {
array_unshift($item, $modx->documentIdentifier);
}
setcookie('last_view', implode(',', $item), time()+60*60*24*30, '/');
}
break;
case 'show':
if (!empty($item)) {
for ($i = 0; $i < count($item); $i++){
$rows .= parseTplChunk($rowTpl, array('id'=>$item[$i]));
}
return parseTplChunk($outerTpl, array('rows'=>$rows, 'outer-class'=>$outerClass));
}
break;
}
?>
Вызываем этот сниппет в шаблоне товара для того, чтобы информация со страниц с этим шаблоном добавлялась в cookie:
[!LastView? &mode=`register`!]
Вызываем сниппет в Ditto в том месте, где будут выводиться последние просмотренные товары:
[!Ditto? &tpl=`last-tpl` &documents=`[!LastView? &mode=`show` !]` &extenders=`nosort`!]
Чанк last-tpl в моем случае выглядел так:
<li class="last-li">
<a href="[~[+id+]~]">
<img src="[+image+]" alt="[+pagetitle+]" title="[+pagetitle+]">
</a>
<div class="thumb-list-item-caption">
<div class="thumb-list-item-title h5">
<a href="[~[+id+]~]">[+pagetitle+]</a>
</div>
<p class="thumb-list-item-price"><span class="shk-price">[+price+]</span> руб.</p>
</div>
</li>
Получаем такой результат:
Создаем сниппет LastView:
<?php
$mode = isset($mode) ? $mode : 'show';
$outerTpl = isset($outerTpl) ? $outerTpl : '@CODE:[+rows+]';
$outerClass = isset($outerClass) ? $outerClass : 'last-view';
$rowTpl = isset($rowTpl) ? $rowTpl : '@CODE:[+id+],';
$maxRows = isset($maxRows) ? $maxRows : 5;
if(!function_exists('fetchTpl')) {
function fetchTpl($tpl){
global $modx;
$template = "";
if(substr($tpl, 0, 6) == "@FILE:") {
$tpl_file = MODX_BASE_PATH . substr($tpl, 6);
$template = file_get_contents($tpl_file);
} else if (substr($tpl, 0, 6) == "@CODE:") {
$template = substr($tpl, 6);
} else if($modx->getChunk($tpl) != "") {
$template = $modx->getChunk($tpl);
} else {
$template = false;
}
return $template;
}
}
if(!function_exists('parseTplChunk')) {
function parseTplChunk($tpl,$placeholder=array()){
$chunk = fetchTpl($tpl);
foreach ($placeholder as $key => $value){
$chunk = str_replace("[+".$key."+]", $value, $chunk);
}
return $chunk;
}
}
$item = array();
if (isset($_COOKIE['last_view']) and $_COOKIE['last_view'] != '') {
$item = explode(',', $_COOKIE['last_view']);
}
switch ($mode) {
case 'register':
if (!in_array($modx->documentIdentifier, $item)) {
if (count($item) >= $maxRows) {
array_pop($item);
array_unshift($item, ($modx->documentIdentifier + ""));
} else {
array_unshift($item, $modx->documentIdentifier);
}
setcookie('last_view', implode(',', $item), time()+60*60*24*30, '/');
}
break;
case 'show':
if (!empty($item)) {
for ($i = 0; $i < count($item); $i++){
$rows .= parseTplChunk($rowTpl, array('id'=>$item[$i]));
}
return parseTplChunk($outerTpl, array('rows'=>$rows, 'outer-class'=>$outerClass));
}
break;
}
?>
Вызываем этот сниппет в шаблоне товара для того, чтобы информация со страниц с этим шаблоном добавлялась в cookie:
[!LastView? &mode=`register`!]
Вызываем сниппет в Ditto в том месте, где будут выводиться последние просмотренные товары:
[!Ditto? &tpl=`last-tpl` &documents=`[!LastView? &mode=`show` !]` &extenders=`nosort`!]
Чанк last-tpl в моем случае выглядел так:
<li class="last-li">
<a href="[~[+id+]~]">
<img src="[+image+]" alt="[+pagetitle+]" title="[+pagetitle+]">
</a>
<div class="thumb-list-item-caption">
<div class="thumb-list-item-title h5">
<a href="[~[+id+]~]">[+pagetitle+]</a>
</div>
<p class="thumb-list-item-price"><span class="shk-price">[+price+]</span> руб.</p>
</div>
</li>
Получаем такой результат:
Просто прекрасно! Спасибо огромное!
ОтветитьУдалитьБлагодарю! Все просто! )
ОтветитьУдалить