/**
  * Install handlers
  */
 public static function installHandlers(ActionEvent $event)
 {
     $currency = \Yii::$app->getModule('seo')->analytics['ecYandex']['currency'];
     if (AnalyticsHandler::CURRENCY_MAIN === intval($currency)) {
         static::$currency = CurrencyHelper::getMainCurrency();
     } elseif (AnalyticsHandler::CURRENCY_USER === intval($currency)) {
         static::$currency = CurrencyHelper::getUserCurrency();
     } else {
         static::$currency = CurrencyHelper::findCurrencyByIso($currency);
     }
     $route = implode('/', [$event->action->controller->module->id, $event->action->controller->id, $event->action->id]);
     Event::on(CartController::className(), CartController::EVENT_ACTION_ADD, [self::className(), 'handleCartAdd'], false);
     Event::on(CartController::className(), CartController::EVENT_ACTION_REMOVE, [self::className(), 'handleRemoveFromCart'], false);
     Event::on(CartController::className(), CartController::EVENT_ACTION_QUANTITY, [self::className(), 'handleChangeQuantity'], false);
     Event::on(CartController::className(), CartController::EVENT_ACTION_CLEAR, [self::className(), 'handleClearCart'], false);
     Event::on(Controller::className(), Controller::EVENT_PRE_DECORATOR, [self::className(), 'handleProductShow']);
     if ('shop/cart/index' === $route) {
         self::handleCartIndex();
     }
     YandexAnalyticsAssets::register(\Yii::$app->getView());
 }
Beispiel #2
0
 /**
  * @return array
  * @throws BadRequestHttpException
  * @throws NotFoundHttpException
  */
 public function actionAdd()
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $products = Yii::$app->request->post('products', []);
     if (false === is_array($products) || true === empty($products)) {
         throw new BadRequestHttpException();
     }
     $order = $this->loadOrder(true);
     if (null === $order->stage || true === $order->getImmutability(Order::IMMUTABLE_USER)) {
         throw new BadRequestHttpException();
     }
     $result = ['products' => [], 'errors' => [], 'additional' => []];
     $result = $this->addProductsToOrder($order, $products, $result);
     $order = $this->loadOrder();
     $order->calculate(true);
     $userCurrency = CurrencyHelper::getUserCurrency();
     $result['success'] = count($products) > 0 && count($result['products']) > 0;
     $result['itemsCount'] = $order->items_count;
     $result['totalPrice'] = $userCurrency->format(CurrencyHelper::convertToUserCurrency($order->total_price, CurrencyHelper::getMainCurrency()));
     $event = new CartActionEvent($order, $result['products']);
     Event::trigger($this, self::EVENT_ACTION_ADD, $event);
     $result['additional'] = $event->getEventData();
     /**
      * Backward compatibility
      */
     $result['itemModalPreview'] = isset($result['additional']['bcItemModalPreview']) ? $result['additional']['bcItemModalPreview'] : '';
     $result['products'] = $this->productsModelsToArray($result['products']);
     return $result;
 }