Skip to content

Instantly share code, notes, and snippets.

@qwertik17
Created June 3, 2019 07:13
Show Gist options
  • Save qwertik17/f0e106c304a10fd88f2842712083baca to your computer and use it in GitHub Desktop.
Save qwertik17/f0e106c304a10fd88f2842712083baca to your computer and use it in GitHub Desktop.
Custom plugin for resources modx revo
<?php
$id = $resource->get('id');
$currentRes = $modx->getObject('modResource', $id);
$template = $currentRes->get('template');
//Шаблон товара "Кухня"
if ($template == 13) {
$cg = 'k_color_groups';
$sg = 'k_style_groups';
//Расчет цены, если не указано в TV
$price = $currentRes->getTVvalue('price');
//if (!$price) {
$price_metr = $currentRes->getTVvalue('price_metr');
$length = $currentRes->getTVvalue('length');
if ($price_metr && $length) {
$price = ceil(($price_metr * $length / 100) / 100) * 100;
$currentRes->setTVvalue('price', $price);
$currentRes->save();
}
//}
//Определение диапазона площади
$area = $currentRes->getTVvalue('area_kitchen');
if($area) {
if ($area < 5) {
$area_range = 'до 5 м<sup>2</sup>';
} elseif ($area < 7) {
$area_range = '5-7 м<sup>2</sup>';
} elseif ($area < 9) {
$area_range = '7-9 м<sup>2</sup>';
} elseif ($area < 12) {
$area_range = '9-12 м<sup>2</sup>';
} else {
$area_range = 'от 12 м<sup>2</sup>';
}
$currentRes->setTVvalue('area_range', $area_range);
$currentRes->save();
}
//Определение диапазона стоимости
$price = $currentRes->getTVvalue('price');
if($k_price) {
if ($price <= 70000) {
$price_range = 'дешевые';
} elseif ($price <= 150000) {
$price_range = 'средние';
} else {
$price_range = 'элитные';
}
$currentRes->setTVvalue('price_range', $k_price_range);
$currentRes->save();
}
}
//Шаблон товара "Шкаф-купе"
if ($template == 18) {
$cg = 's_color_groups';
$sg = 's_style_groups';
}
//Шаблон товара "Гардеробные"
if ($template == 19) {
$cg = 'g_color_groups';
$sg = 'g_style_groups';
}
if (in_array($template, array(13,18,19))) {
$options_page = $modx->getObject('modResource', 25);
//Определение группы цветов
$currentRes_colors = explode('||', $currentRes->getTVvalue('color'));
$colors_page_groups = json_decode($options_page->getTVvalue($cg),true);
$groups = array();
foreach($colors_page_groups as $group) {
$title = $group['title'];
$colors = $group['colors'];
if (is_array($colors)) {
foreach($colors as $color) {
if (in_array($color, $currentRes_colors)) {
$groups[] = $title;
break;
}
}
} else {
if (in_array($colors, $currentRes_colors)) {
$groups[] = $title;
}
}
}
$currentRes->setTVvalue('color_group', implode('||',$groups));
$currentRes->save();
//Определение группы стилей
$currentRes_styles = explode('||', $currentRes->getTVvalue('style'));
$styles_page_groups = json_decode($options_page->getTVvalue($sg),true);
$groups = array();
foreach($styles_page_groups as $group) {
$title = $group['group'];
$styles = $group['styles'];
if (is_array($styles)) {
foreach($styles as $style) {
if (in_array($style, $currentRes_styles)) {
$groups[] = $title;
break;
}
}
} else {
if (in_array($styles, $currentRes_styles)) {
$groups[] = $title;
}
}
}
$currentRes->setTVvalue('style_group', implode('||',$groups));
$currentRes->save();
if($currentRes->getTVvalue('priority') == null) {
$currentRes->setTVvalue('priority', 0);
$currentRes->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment