Skip to content

Instantly share code, notes, and snippets.

@Fi1osof
Created December 24, 2013 05:38
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 Fi1osof/74eb06374952217c4173 to your computer and use it in GitHub Desktop.
Save Fi1osof/74eb06374952217c4173 to your computer and use it in GitHub Desktop.
<?php
/*
Получаем данные корзины
*/
require_once MODX_CORE_PATH . 'components/shopmodx/processors/web/getdata.class.php';
class modBasketMgrOrdersProductsGetdataProcessor extends ShopmodxWebGetlistProcessor{
public $classKey = 'OrderProduct';
protected $sum = 0; // Общая сумма заказа
protected $positions = 0; // Количество позиций
protected $quantity = 0; // Общее количество товаров
protected $weight = 0; // Общий вес заказа
protected $height = 0; // Общая высота заказа
protected $width = 0; // Общая ширина
protected $length = 0; // Общая длина заказа
public function prepareQueryBeforeCount(xPDOQuery $c){
$c = parent::prepareQueryBeforeCount($c);
$where = array();
if($order_id = (int)$this->getProperty('order_id')){
$where['order_id'] = $order_id;
}
if(!$this->getProperty('show_canceled', false)){
$where['quantity:>'] = 0;
}
if($where){
$c->where($where);
}
return $c;
}
public function setSelection(xPDOQuery $c){
$c = parent::setSelection($c);
$c->innerJoin('ShopmodxProduct', 'Product');
$c->select(array(
"Product.resource_id",
));
return $c;
}
public function afterIteration(array $list){
$list = parent::afterIteration($list);
$this->quantity = 0;
$this->sum = 0;
/*
Ширину и длину посылки - берем максимальные значения среди данных товаров
*/
$width = 0;
$length = 0;
// Получаем id всех товаров и подсчитываем общее число товаров и сумму
$ids = array();
foreach($list as $l){
$this->quantity += $l['quantity'];
$this->sum += $l['quantity'] * $l['price'];
$ids[] = $l['resource_id'];
// Получаем вес в килограммах
$this->weight += ( !empty($l['tvs']['weight']['value']) ? $l['tvs']['weight']['value'] : 100) * $l['quantity'] / 1000;
// Получаем общую высоту посылки
$this->height += (!empty($l['tvs']['height']['value']) ? $l['tvs']['height']['value'] : 2) * $l['quantity'];
$w = (!empty($l['tvs']['width']['value']) ? $l['tvs']['width']['value'] : 30);
$l = (!empty($l['tvs']['length']['value']) ? $l['tvs']['length']['value'] : 10);
if($w > $width){
$width = $w;
}
if($l > $length){
$length = $l;
}
}
$this->width = $width;
$this->length = $length;
// Получаем данные товаров
$resources = array();
if($ids){
$ids = array_unique($ids);
if(
$response = $this->modx->runProcessor('web/catalog/products/getdata', array(
"limit" => 0,
"showhidden" => true,
"where" => array(
"id:in" => $ids,
),
), array(
'processors_path' => MODX_CORE_PATH . 'components/modxsite/processors/',
))
AND $resources = $response->getObject())
{
// print_r($response->getResponse());
foreach($list as & $l){
if(!empty($resources[$l['resource_id']])){
$l = array_merge($resources[$l['resource_id']], $l);
}
}
}
}
return $list;
}
public function outputArray(array $array, $count = false){
$result = parent::outputArray($array, $count);
$result['sum'] = $this->sum;
$result['quantity'] = $this->quantity;
$result['positions'] = $count;
$result['weight'] = $this->weight;
$result['height'] = $this->height;
$result['width'] = $this->width;
$result['length'] = $this->length;
return $result;
}
}
return 'modBasketMgrOrdersProductsGetdataProcessor';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment