Skip to content

Instantly share code, notes, and snippets.

@Sentinel-7
Forked from grachov/msOrder
Created March 3, 2020 07:49
Show Gist options
  • Save Sentinel-7/e43749ac1bb8749ae31b9671c5ad6810 to your computer and use it in GitHub Desktop.
Save Sentinel-7/e43749ac1bb8749ae31b9671c5ad6810 to your computer and use it in GitHub Desktop.
Простой способ для пересчета стоимости заказа после его изменения
<?php
class msOrder extends xPDOSimpleObject
{
public function updateProducts()
{
$originalContext = $this->xpdo->context->get('key');
$this->xpdo->switchContext($this->get('context'));
$originalMiniShop = isset($this->xpdo->services['minishop2']) ? $this->xpdo->services['minishop2'] : null;
$cart = array();
foreach ($this->getMany('Products') as $product) {
$product = array(
'id' => $product->get('product_id'),
'price' => $product->get('price'),
'weight' => $product->get('weight'),
'count' => $product->get('count'),
'options' => $this->xpdo->fromJSON($product->get('options')),
'ctx' => $this->get('context'),
);
if (!is_array($product['options'])) {
$product['options'] = array();
}
$cart[md5($product['id'] . $product['price'] . $product['weight'] . $this->xpdo->toJSON($product['options']))] = $product;
}
$address = $this->getOne('Address');
/**
* @var miniShop2 $miniShop2
*/
$miniShop2 = $this->xpdo->services['minishop2'] = new miniShop2($this->xpdo, array(
'cart' => $cart,
'order' => array_merge($address ? $address->toArray() : array(), $this->toArray()),
));
$miniShop2->initialize($this->get('context'));
$cartStatus = $miniShop2->cart->status();
$deliveryCost = $miniShop2->order->getCost(false, true);
$this->fromArray(array(
'cart_cost' => $cartStatus['total_cost'],
'weight' => $cartStatus['total_weight'],
'delivery_cost' => $deliveryCost,
'cost' => $cartStatus['total_cost'] + $deliveryCost,
));
$this->xpdo->services['minishop2'] = $originalMiniShop;
$this->xpdo->switchContext($originalContext);
return $this->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment