Skip to content

Instantly share code, notes, and snippets.

@andronex
Created July 13, 2019 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andronex/99ee1ba327638aff458efae9b01ef5e6 to your computer and use it in GitHub Desktop.
Save andronex/99ee1ba327638aff458efae9b01ef5e6 to your computer and use it in GitHub Desktop.
Изменение цены в зависимости от выбранных опций товара (miniShop2 + MODX Revolution).
<?php
switch ($modx->event->name){
case 'msOnBeforeAddToCart':
if (!is_array($options)) $options = json_decode($options, true);
//$modx->log(modX::LOG_LEVEL_ERROR, print_r($cart->get(), true));
//$modx->log(modX::LOG_LEVEL_ERROR, print_r($product->toArray(), true));
if(isset($options['size-price']) && !empty($options['size-price'])){
$tvr = $modx->getObject('modTemplateVarResource', array(
'tmplvarid' => 53,
'contentid' => $product->id
));
if ($tvr) {
$array = json_decode($tvr->get('value'), true);
$key = array_search($options['size-price'], array_column($array, 'size'));
$product->set('price', $array[$key]['price']);
}
}
if (isset($options['colors-price'])) {
$price = $product->get('price') + ($options['colors-price']);
$product->set('price', $price);
}
if (isset($options['colors-obivka-price'])) {
$price = $product->get('price') + ($options['colors-obivka-price']);
$product->set('price', $price);
}
if (isset($options['colors-legs-price'])) {
$price = $product->get('price') + ($options['colors-legs-price']);
$product->set('price', $price);
}
if (isset($options['complected-id'])) {
$items_complect = explode(',', $options['complected-id']);
$prices = 0;
foreach($items_complect as $item){
$item = explode(':', $item);
$count = $item[1];
$idgoods = $item[0];
if($goods = $modx->getObject('msProduct', (int)$idgoods)){
$data = $goods->getOne('Data');
$price = $data->price * $count;
$prices = $prices + $price;
}
}
$price = $product->get('price') + $prices;
$product->set('price', $price);
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment