public function save($validate = true) { if ($this->validate($validate) && !Yii::$app->cart->isEmpty) { $transaction = Yii::$app->db->beginTransaction(); $this->productOrder->attributes = ['user_id' => Yii::$app->user->identity->id, 'address' => $this->address, 'contact' => $this->contact, 'payment' => $this->payment, 'shipment' => $this->shipment, 'total_price' => Yii::$app->cart->cost, 'status' => ProductOrder::STATUS_CREATED]; if (!$this->productOrder->save()) { return false; } foreach (Yii::$app->cart->positions as $productInCart) { $productOrderCompose = new ProductOrderCompose(); $productOrderCompose->attributes = ['order_id' => $this->productOrder->id, 'user_id' => Yii::$app->user->identity->id, 'product_id' => $productInCart->id, 'product_attribute_option_ids' => ProductInCart::getSerializedAttributeAssignments($productInCart->attributeAssignments), 'product_count' => $productInCart->quantity, 'settlement_price' => $productInCart->cost]; if (!$productOrderCompose->save(true)) { return false; } } $transaction->commit(); Yii::$app->cart->removeAll(); return true; } return false; }
public static function canComment($userId, $productId) { return ProductOrderCompose::find()->where(['user_id' => $userId, 'product_id' => $productId])->exists(); }