Skip to content

Instantly share code, notes, and snippets.

@grachov
Created May 3, 2015 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save grachov/7337b6ec98c05fbc4926 to your computer and use it in GitHub Desktop.
Save grachov/7337b6ec98c05fbc4926 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();
}
}
@yujj
Copy link

yujj commented Aug 4, 2015

Предлагаю модифицировать этот способ, чтобы он не требовал изменения кода. Кроме того, работал бы не только при обновлении товаров, но и при изменении способа доставки.

Создаём плагин на событие msOnBeforeUpdateOrder:

switch ($modx->event->name) {
    case 'msOnBeforeUpdateOrder':

        $originalContext = $object->xpdo->context->get('key');
        $object->xpdo->switchContext($object->get('context'));
        $originalMiniShop = isset($object->xpdo->services['minishop2']) ? $object->xpdo->services['minishop2'] : null;
        $cart             = array();
        foreach ($object->getMany('Products') as $product) {
            $product = array(
                'id' => $product->get('product_id'),
                'price' => $product->get('price'),
                'weight' => $product->get('weight'),
                'count' => $product->get('count'),
                'options' => $object->xpdo->fromJSON($product->get('options')),
                'ctx' => $object->get('context')
            );
            if (!is_array($product['options'])) {
                $product['options'] = array();
            }
            $cart[md5($product['id'] . $product['price'] . $product['weight'] . $object->xpdo->toJSON($product['options']))] = $product;
        }
        $address   = $object->getOne('Address');
        /**
         * @var miniShop2 $miniShop2
         */
        $miniShop2 = $object->xpdo->services['minishop2'] = new miniShop2($object->xpdo, array(
            'cart' => $cart,
            'order' => array_merge($address ? $address->toArray() : array(), $object->toArray())
        ));
        $miniShop2->initialize($object->get('context'));
        $cartStatus   = $miniShop2->cart->status();
        $deliveryCost = $miniShop2->order->getCost(false, true);
        $object->fromArray(array(
            'cart_cost' => $cartStatus['total_cost'],
            'weight' => $cartStatus['total_weight'],
            'delivery_cost' => $deliveryCost,
            'cost' => $cartStatus['total_cost'] + $deliveryCost
        ));
        $object->xpdo->services['minishop2'] = $originalMiniShop;
        $object->xpdo->switchContext($originalContext);

        return true;

        break;
}

Т.е. в Вашем коде $this теперь заменён на $object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment