-
-
Save 0test/6100642ceaf9ae3bc298cc7a98259274 to your computer and use it in GitHub Desktop.
tovarparams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //в категории и товаре отдельно получаем список параметров, с которыми будем манипулировать | |
| //для категории - с птичкой "показывать в списке" | |
| // $this->data['categoryTvList'] = Helper::getCategoryTvList($this->docid, 'list_yes'); | |
| //в вызове DocLister/eFilterResult | |
| // 'tvList' => Helper::productsTvList($docid), | |
| //для конечного продукта - все для родительской категории | |
| // $this->data['productTvList'] = = Helper::getCategoryTvList( evo()->documentObject['parent'] ); | |
| //сами методы класса Helper | |
| class Helper | |
| { | |
| //id tv tovarparams | |
| public static $tovarparamsTvId = 30; | |
| //список id шаблонов "Продукт" | |
| public static $productTempatesIds = '14'; | |
| public static function productsTvList($docid = 2) | |
| { | |
| if(!empty($docid)) { | |
| $categoryTvs = self::getCategoryTvList($docid, 'list_yes'); | |
| } | |
| //тут задаем свои общие для всех товаров, плюс докидываем полученные для конкретной категории | |
| return 'image,price,old_price,is_new,is_action,in_stock,articul,brand' . ((!empty($categoryTvs)) ? ',' . implode(',', array_keys($categoryTvs)) : ''); | |
| } | |
| public static function getCategoryTvList($docid, $filter = '') | |
| { | |
| $tvid = self::$tovarparamsTvId; | |
| $arr = Cache::rememberForever('CategoryTvList_' . $docid . '_' . $tvid . '_' . $filter, function() use($docid, $tvid, $filter){ | |
| $tvs = []; | |
| $parents = evo()->getParentIds($docid); | |
| if(!empty($parents)) { | |
| $tmp = array_keys($parents); | |
| $tmp[] = end($parents); | |
| $parents = $tmp; | |
| } | |
| array_unshift($parents, $docid); | |
| array_unshift($parents, evo()->documentIdentifier); | |
| if(!empty($parents)) { | |
| $res = SiteTmplvarContentvalue::select(['contentid', 'value']) | |
| ->whereIn('contentid', $parents)->where('tmplvarid', $tvid) | |
| ->orderByRaw("FIND_IN_SET (contentid, '" . implode(',', $parents) . "')") | |
| ->get()->toArray(); | |
| foreach($res as $row) { | |
| $tv = json_decode($row['value'], 1); | |
| if(!empty($tv['fieldValue'])) { | |
| $arr = $tv['fieldValue']; | |
| break; | |
| } | |
| } | |
| } | |
| unset($res); | |
| if(!empty($arr)) { | |
| if(!empty($filter)) { | |
| $arr = array_filter($arr, function ($a) use($filter) { | |
| return !empty($a[ $filter ]); | |
| }); | |
| } | |
| if(!empty($arr)) { | |
| $res = SiteTmplvar::whereIn('id', array_column($arr, 'param_id')) | |
| //->where('category', 8) | |
| ->get()->keyBy('id')->toArray(); | |
| foreach($res as &$row) { | |
| if(!empty($row['elements'])) { | |
| $row['elements'] = self::convertElementToArray($row['elements']); | |
| } else { | |
| if($row['type'] == 'custom_tv:selector') { | |
| $row['elements'] = self::convertElementForSelector($row['name']); | |
| } else { | |
| $row['elements'] = []; | |
| } | |
| } | |
| } | |
| foreach($arr as $row2) { | |
| if(isset($res[ $row2['param_id'] ])) { | |
| $tvs[ $res[ $row2['param_id'] ]['name'] ] = $res[ $row2['param_id'] ]; | |
| } | |
| } | |
| } | |
| } | |
| return $tvs; | |
| }); | |
| return $arr; | |
| } | |
| public static function convertElementToArray($elements) | |
| { | |
| //функция для преобразования возможных значений тв в массив (чтобы можно было их потом выводить) | |
| $arr = []; | |
| $tmp = explode('||', trim($elements, '| ')); | |
| foreach($tmp as $str) { | |
| $tmp2 = explode('==', $str); | |
| $arr[ $tmp2[1] ?? $tmp2[0] ] = $tmp2[0]; | |
| } | |
| return $arr; | |
| } | |
| public static function convertElementForSelector($tvname) | |
| { | |
| //функция для преобразования возможных значений тв типа селектор в массив (чтобы можно было их потом выводить) | |
| //тут придется прописать самому, чтобы не брать все дерево, но их обычно немного | |
| $arr = []; | |
| switch(true) { | |
| case 'brand': | |
| $arr = Cache::rememberForever('ElementForSelector_' . $tvname, function(){ | |
| return SiteContent::select(['id', 'pagetitle']) | |
| ->where('parent', 99)->get() | |
| ->pluck('pagetitle', 'id')->toArray(); | |
| }); | |
| break; | |
| default: | |
| break; | |
| } | |
| return $arr; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //вывод в продукте | |
| @foreach($productTvList as $tvname => $row) | |
| @if(!empty($$tvname)) | |
| <div class="elm-row"> | |
| <div class="row-title">{{ $row['caption'] }}</div> | |
| <div class="row-value"> | |
| <?php | |
| $values = $row['type'] == 'custom_tv:selector' ? explode(',', $$tvname) : explode('||', $$tvname); | |
| $tmp = []; | |
| foreach($values as $v) { | |
| $tmp[] = $row['elements'][$v] ?? $v; | |
| } | |
| ?> | |
| {!! implode(',', $tmp) !!} | |
| </div> | |
| </div> | |
| @endif | |
| @endforeach |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //вывод в списке продуктов (в категории) | |
| @foreach($categoryTvList ?? [] as $tvname => $data) | |
| @if(!empty($item['tv_' . $tvname])) | |
| <div class="elm-row"> | |
| <div class="row-title">{{ $data['caption'] }}:</div> | |
| <div class="row-value"> | |
| <?php | |
| $values = $data['type'] == 'custom_tv:selector' ? explode(',', $item['tv_' . $tvname]) : explode('||', $item['tv_' . $tvname]); | |
| $tmp = []; | |
| foreach($values as $v) { | |
| $tmp[] = $data['elements'][$v] ?? $v; | |
| } | |
| ?> | |
| {!! implode(',', $tmp) !!} | |
| </div> | |
| </div> | |
| @endif | |
| @endforeach |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment