-
-
Save Sentinel-7/e43749ac1bb8749ae31b9671c5ad6810 to your computer and use it in GitHub Desktop.
Простой способ для пересчета стоимости заказа после его изменения
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 | |
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