Created
August 24, 2015 11:07
-
-
Save vgrish/e987bc8119b98c0348a7 to your computer and use it in GitHub Desktop.
<option value="[[+value]]" [[+selected]]>[[+value]] - [[+price]] - [[+total_price]] - [[+operation]]</option>
This file contains 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
<?php | |
$msop2 = $modx->getService('msop2', 'msop2', $modx->getOption('msop2_core_path', null, $modx->getOption('core_path') . 'components/msop2/') . 'model/msop2/', $scriptProperties); | |
if (!($msop2 instanceof msop2)) return ''; | |
$msop2Option = 0; | |
if(!$msop2Option = $modx->getObject('msop2Option', $name)) { | |
$msop2Option = $msop2Option->get('id'); | |
} | |
$miniShop2 = $modx->getService('minishop2'); | |
/* @var array $scriptProperties */ | |
/* @var pdoFetch $pdoFetch */ | |
if (!$modx->loadClass('pdofetch', MODX_CORE_PATH . 'components/pdotools/model/pdotools/', false, true)) {return false;} | |
$pdoFetch = new pdoFetch($modx, $scriptProperties); | |
if (empty($product) && !empty($input)) {$product = $input;} | |
if (empty($selected)) {$selected = '';} | |
if (empty($outputSeparator)) {$outputSeparator = "\n";} | |
if ((empty($name) || $name == 'id') && !empty($options)) {$name = $options;} | |
$output = ''; | |
$product = !empty($product) ? $modx->getObject('msProduct', $product) : $product = $modx->resource; | |
$productId = $product->get('id'); | |
$productPrice = $product->get('price'); | |
if (!($product instanceof msProduct)) { | |
$output = 'This resource is not instance of msProduct class.'; | |
} | |
elseif (!empty($name) && $options = $product->get($name)) { | |
if (!is_array($options) || $options[0] == '') { | |
$output = !empty($tplEmpty) | |
? $pdoFetch->getChunk($tplEmpty, $scriptProperties) | |
: ''; | |
} | |
else { | |
$rows = array(); | |
foreach ($options as $value) { | |
$pls = array(); | |
$pls['price'] = 0; | |
$pls['total_price'] = 0; | |
$pls['operation'] = ''; | |
$pls['percent'] = 0; | |
if ($price = $modx->getObject('msop2Price', array( | |
'product_id' => $productId, | |
'option' => $msop2Option, | |
'value' => $value | |
))) { | |
$pls['price'] = $price->get('price'); | |
$pls['operation'] = $price->get('operation'); | |
switch ($pls['operation']) { | |
default: | |
case '1': | |
$pls['total_price'] = $pls['price']; | |
$pls['operation'] = '='; | |
break; | |
case '2': | |
$pls['total_price'] = $productPrice + $pls['price']; | |
$pls['operation'] = '+'; | |
break; | |
case '3': | |
$pls['total_price'] = $productPrice - $pls['price']; | |
$pls['operation'] = '-'; | |
break; | |
} | |
} | |
$pls['price'] = $miniShop2->formatPrice($pls['price']); | |
$pls['total_price'] = $miniShop2->formatPrice($pls['total_price']); | |
$pls['value'] = $value; | |
$pls['selected'] = $value == $selected ? 'selected' : ''; | |
$rows[] = empty($tplRow) ? $value : $pdoFetch->getChunk($tplRow, $pls); | |
} | |
$rows = implode($outputSeparator, $rows); | |
$output = empty($tplOuter) | |
? $pdoFetch->getChunk('', array('name' => $name, 'rows' => $rows)) | |
: $pdoFetch->getChunk($tplOuter, array_merge($scriptProperties, array('name' => $name, 'rows' => $rows))); | |
} | |
} | |
return $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment