Created
April 18, 2018 11:12
-
-
Save pavel-one/e7be87f2121a24cc0168d818c7da6eea to your computer and use it in GitHub Desktop.
Переделанная корзина minishop2 для запроса по ajax {'!getCartAjax' | snippet | json_encode}
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 | |
/** @var modX $modx */ | |
/** @var array $scriptProperties */ | |
/** @var miniShop2 $miniShop2 */ | |
$miniShop2 = $modx->getService('miniShop2'); | |
$miniShop2->initialize($modx->context->key); | |
/** @var pdoFetch $pdoFetch */ | |
if (!$modx->loadClass('pdofetch', MODX_CORE_PATH . 'components/pdotools/model/pdotools/', false, true)) { | |
return false; | |
} | |
$pdoFetch = new pdoFetch($modx, $scriptProperties); | |
$pdoFetch->addTime('pdoTools loaded.'); | |
$cart = $miniShop2->cart->get(); | |
$status = $miniShop2->cart->status(); | |
// Select cart products | |
$where = array( | |
'msProduct.id:IN' => array(), | |
); | |
foreach ($cart as $entry) { | |
$where['msProduct.id:IN'][] = $entry['id']; | |
} | |
$where['msProduct.id:IN'] = array_unique($where['msProduct.id:IN']); | |
// Include products properties | |
$leftJoin = array( | |
'Data' => array( | |
'class' => 'msProductData', | |
), | |
'Vendor' => array( | |
'class' => 'msVendor', | |
'on' => 'Data.vendor = Vendor.id', | |
), | |
); | |
// Select columns | |
$select = array( | |
'msProduct' => !empty($includeContent) | |
? $modx->getSelectColumns('msProduct', 'msProduct') | |
: $modx->getSelectColumns('msProduct', 'msProduct', '', array('content'), true), | |
'Data' => $modx->getSelectColumns('msProductData', 'Data', '', array('id'), true), | |
'Vendor' => $modx->getSelectColumns('msVendor', 'Vendor', 'vendor.', array('id'), true), | |
); | |
// Include products thumbnails | |
if (!empty($includeThumbs)) { | |
$thumbs = array_map('trim', explode(',', $includeThumbs)); | |
if (!empty($thumbs[0])) { | |
foreach ($thumbs as $thumb) { | |
$leftJoin[$thumb] = array( | |
'class' => 'msProductFile', | |
'on' => "`{$thumb}`.product_id = msProduct.id AND `{$thumb}`.parent != 0 AND `{$thumb}`.path LIKE '%/{$thumb}/%' AND `{$thumb}`.rank = 0", | |
); | |
$select[$thumb] = "`{$thumb}`.url as '{$thumb}'"; | |
} | |
$pdoFetch->addTime('Included list of thumbnails: <b>' . implode(', ', $thumbs) . '</b>.'); | |
} | |
} | |
// Add user parameters | |
foreach (array('where', 'leftJoin', 'select') as $v) { | |
if (!empty($scriptProperties[$v])) { | |
$tmp = $scriptProperties[$v]; | |
if (!is_array($tmp)) { | |
$tmp = json_decode($tmp, true); | |
} | |
if (is_array($tmp)) { | |
$$v = array_merge($$v, $tmp); | |
} | |
} | |
unset($scriptProperties[$v]); | |
} | |
$pdoFetch->addTime('Conditions prepared'); | |
$default = array( | |
'class' => 'msProduct', | |
'where' => $where, | |
'leftJoin' => $leftJoin, | |
'select' => $select, | |
'sortby' => 'msProduct.id', | |
'sortdir' => 'ASC', | |
'groupby' => 'msProduct.id', | |
'limit' => 0, | |
'return' => 'data', | |
'nestedChunkPrefix' => 'minishop2_', | |
); | |
// Merge all properties and run! | |
$pdoFetch->setConfig(array_merge($default, $scriptProperties), false); | |
$tmp = $pdoFetch->run(); | |
$rows = array(); | |
foreach ($tmp as $row) { | |
$rows[$row['id']] = $row; | |
} | |
// Process products in cart | |
$products = array(); | |
$total = array('count' => 0, 'weight' => 0, 'cost' => 0, 'total_OldPrice' => 0, 'total_discount' => 0); | |
foreach ($cart as $key => $entry) { | |
if (!isset($rows[$entry['id']])) { | |
continue; | |
} | |
$product = $rows[$entry['id']]; | |
$product['key'] = $key; | |
$product['count'] = $entry['count']; | |
$product['old_price'] = $miniShop2->formatPrice( | |
$product['price'] > $entry['price'] | |
? $product['price'] | |
: $product['old_price'] | |
); | |
$product['price'] = $miniShop2->formatPrice($entry['price']); | |
$product['weight'] = $miniShop2->formatWeight($entry['weight']); | |
$product['cost'] = $miniShop2->formatPrice($entry['count'] * $entry['price']); | |
if ($product['old_price']) { | |
$product['discount'] = $miniShop2->formatPrice(str_replace(' ', '', $product['old_price']) * $product['count'] - $product['count'] * str_replace(' ', '',$product['price'])); | |
$product['total_oldPrice'] = $miniShop2->formatPrice(str_replace(' ', '', $product['old_price']) * $product['count']); | |
} else { | |
$product['discount'] = 0; | |
$product['total_oldPrice'] = 0; | |
} | |
$product['total_price'] = $miniShop2->formatPrice(str_replace(' ', '', $product['price']) * $product['count']); | |
// Additional properties of product in cart | |
if (!empty($entry['options']) && is_array($entry['options'])) { | |
$product['options'] = $entry['options']; | |
foreach ($entry['options'] as $option => $value) { | |
$product['option.' . $option] = $value; | |
} | |
} | |
// Add option values | |
$options = $modx->call('msProductData', 'loadOptions', array(&$modx, $product['id'])); | |
$products[] = array_merge($product, $options); | |
// Count total | |
$total['count'] += $entry['count']; | |
$total['cost'] += $entry['count'] * $entry['price']; | |
$total['weight'] += $entry['count'] * $entry['weight']; | |
//wtf | |
$total['total_OldPrice'] += str_replace(' ', '',$product['total_oldPrice']); | |
if ($product['total_oldPrice']) { | |
$total['total_discount'] += str_replace(' ', '', $product['total_oldPrice']); | |
} else { | |
$total['total_discount'] += str_replace(' ', '', $product['total_price']); | |
} | |
//$modx->log(1, $total['total_discount']); | |
} | |
if ($total['total_discount'] == $total['cost']) { | |
$total['total_discount'] = 0; | |
} | |
if ($total['total_discount']) { | |
$total['total_discount_two'] = $total['total_discount'] - $total['cost']; | |
} | |
if ($total['total_discount']) { | |
$total['discount_percent'] = $total['total_discount'] / $total['total_discount_two']; | |
$total['discount_percent'] = round(100 / $total['discount_percent'], 0); | |
} | |
$total['total_OldPrice'] = $miniShop2->formatPrice($total['total_OldPrice']); | |
$total['total_discount'] = $miniShop2->formatPrice($total['total_discount']); | |
$total['cost'] = $miniShop2->formatPrice($total['cost']); | |
$total['weight'] = $miniShop2->formatWeight($total['weight']); | |
$output = array( | |
'total' => $total, | |
'products' => $products, | |
); | |
if ($scriptProperties['tpl']) | |
return $pdoFetch->getChunk($scriptProperties['tpl'], $output); | |
else { | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment