コード例 #1
0
 public function actionAddProduct($orderId, $productId, $parentId = 0)
 {
     $order = $this->findModel($orderId);
     /** @var OrderItem $orderItem */
     $orderItem = OrderItem::findOne(['product_id' => $productId, 'order_id' => $orderId]);
     /** @var Product $product */
     $product = Product::findById($productId);
     if (is_null($orderItem)) {
         $orderItem = new OrderItem();
         $orderItem->attributes = ['parent_id' => $parentId, 'order_id' => $order->id, 'product_id' => $product->id, 'quantity' => $product->measure->nominal, 'price_per_pcs' => PriceHelper::getProductPrice($product, $order, 1, SpecialPriceList::TYPE_CORE)];
     } else {
         $orderItem->quantity++;
     }
     $orderItem->save();
     $order->calculate(true);
     return $this->redirect(['view', 'id' => $orderId]);
 }
コード例 #2
0
 protected function addProductsToOrder($products, $parentId = 0)
 {
     if (!is_array($products)) {
         throw new BadRequestHttpException();
     }
     $order = $this->loadOrder(true);
     $result = ['errors' => [], 'itemModalPreview' => ''];
     foreach ($products as $product) {
         if (!isset($product['id']) || is_null($productModel = Product::findById($product['id']))) {
             $result['errors'][] = Yii::t('app', 'Product not found.');
             continue;
         }
         /** @var Product $productModel */
         $quantity = isset($product['quantity']) && (double) $product['quantity'] > 0 ? (double) $product['quantity'] : 1;
         if ($this->module->allowToAddSameProduct || is_null($orderItem = OrderItem::findOne(['order_id' => $order->id, 'product_id' => $productModel->id, 'parent_id' => 0]))) {
             $orderItem = new OrderItem();
             $orderItem->attributes = ['parent_id' => $parentId, 'order_id' => $order->id, 'product_id' => $productModel->id, 'quantity' => $quantity, 'price_per_pcs' => PriceHelper::getProductPrice($productModel, $order, 1, SpecialPriceList::TYPE_CORE)];
         } else {
             /** @var OrderItem $orderItem */
             $orderItem->quantity += $quantity;
         }
         if (!$orderItem->save()) {
             $result['errors'][] = Yii::t('app', 'Cannot save order item.');
         } else {
             // refresh order
             Order::clearStaticOrder();
             $order = $this->loadOrder(false);
         }
         if (isset($product['children'])) {
             $result = ArrayHelper::merge($result, $this->addProductsToOrder($product['children'], $orderItem->id));
         }
         if ($parentId === 0) {
             $result['itemModalPreview'] .= $this->renderPartial('item-modal-preview', ['order' => $order, 'orderItem' => $orderItem, 'product' => $product]);
         }
     }
     if ($parentId === 0) {
         $order->calculate(true);
     }
     $mainCurrency = Currency::getMainCurrency();
     return ArrayHelper::merge($result, ['itemsCount' => $order->items_count, 'success' => true, 'totalPrice' => $mainCurrency->format($order->total_price)]);
 }
コード例 #3
0
ファイル: CartController.php プロジェクト: lzpfmh/dotplant2
 /**
  * @param Order $order
  * @param array $products
  * @param $result
  * @param int $parentId
  * @return mixed
  * @throws BadRequestHttpException
  * @throws NotFoundHttpException
  */
 protected function addProductsToOrder(Order $order, $products = [], $result, $parentId = 0)
 {
     $parentId = intval($parentId);
     if ($parentId !== 0) {
         // if parent id is set - order item should exist in this order!
         $parentOrderItem = OrderItem::findOne(['order_id' => $order->id, 'id' => $parentId]);
         if ($parentOrderItem === null) {
             throw new BadRequestHttpException();
         }
     }
     foreach ($products as $product) {
         $productModel = $addonModel = null;
         if (!isset($product['id'])) {
             if (isset($product['addon_id'])) {
                 $addonModel = Addon::findById($product['addon_id']);
             }
         } else {
             $productModel = Product::findById($product['id']);
         }
         if ($addonModel === null && $productModel === null) {
             $result['errors'][] = Yii::t('app', 'Product not found.');
             continue;
         }
         /** @var Product $productModel */
         $quantity = isset($product['quantity']) && (double) $product['quantity'] > 0 ? (double) $product['quantity'] : 1;
         $condition = ['order_id' => $order->id, 'parent_id' => 0];
         if ($productModel !== null) {
             $condition['product_id'] = $productModel->id;
             $thisItemModel = $productModel;
             $quantity = $productModel->measure->ceilQuantity($quantity);
         } else {
             $condition['addon_id'] = $addonModel->id;
             $thisItemModel = $addonModel;
             if (!$addonModel->can_change_quantity) {
                 $quantity = 1;
             }
         }
         $orderItem = OrderItem::findOne($condition);
         if ($this->module->allowToAddSameProduct || null === $orderItem) {
             $orderItem = new OrderItem();
             $orderItem->attributes = ['parent_id' => $parentId, 'order_id' => $order->id, 'quantity' => $quantity, 'price_per_pcs' => PriceHelper::getProductPrice($thisItemModel, $order, 1, SpecialPriceList::TYPE_CORE)];
             if (empty($product['customName']) === false) {
                 $orderItem->custom_name = $product['customName'];
             }
             if ($productModel !== null) {
                 $orderItem->product_id = $thisItemModel->id;
             } else {
                 $orderItem->addon_id = $thisItemModel->id;
             }
         } else {
             /** @var OrderItem $orderItem */
             if ($addonModel !== null && !$addonModel->can_change_quantity) {
                 // quantity can not be changed
                 $quantity = 0;
             }
             if (null !== $orderItem) {
                 $orderItem->quantity += $quantity;
             }
         }
         if (false === $orderItem->save()) {
             $result['errors'][] = Yii::t('app', 'Cannot save order item.');
         } else {
             // refresh order
             Order::clearStaticOrder();
             $order = $this->loadOrder(false);
         }
         if (null !== $productModel) {
             $result['products'][] = ['model' => $productModel, 'quantity' => $quantity, 'orderItem' => $orderItem];
         }
         if (isset($product['children']) && is_array($product['children'])) {
             $result = $this->addProductsToOrder($order, $product['children'], $result, $orderItem->id);
         }
     }
     return $result;
 }