Пример #1
0
 public function put($items = [])
 {
     $order = null;
     $cartGuid = Yii::$app->session->get('cartGuid');
     if ($cartGuid) {
         $order = CartOrder::find()->where('guid = :guid ', ['guid' => $cartGuid])->one();
     }
     if (!$order || !$cartGuid) {
         $cartGuid = Utils::createGuid();
         Yii::$app->session->set('cartGuid', $cartGuid);
         $order = new CartOrder();
         $ts = time();
         $order->created_at = $ts;
         $order->updated_at = $ts;
         $order->guid = $cartGuid;
         $order->device_id = $this->device->id;
         if (!$order->save()) {
             Yii::error($order->getErrors(), __LINE__);
         }
     }
     $rows = [];
     foreach ($items as $item) {
         if (!isset($item['rowId'])) {
             $rowId = Utils::createGuid($cartGuid);
             $item['rowId'] = $rowId;
             $item['orderId'] = $order->id;
             $this->insertRow($item);
             $rows[] = ['rowId' => $rowId, 'productId' => $item['productId'], 'id' => $item['id']];
         } else {
             if ($item['qty'] <= 0) {
                 $rowsCount = CartItem::deleteAll('row_id = :row_id AND order_id = :order_id', [':row_id' => $item['rowId'], ':order_id' => $order['id']]);
             } else {
                 $item['orderId'] = $order->id;
                 $this->changeQtyRow($item);
                 $rows[] = ['rowId' => $item['rowId'], 'productId' => $item['productId'], 'id' => $item['id']];
             }
         }
     }
     $this->updateOrderTotal($order->id);
     return ['rows' => $rows];
 }