/** * @param array $coupons * @return bool */ public function applyCoupons(array $coupons) { if (!$this->isCouponsAvailable()) { return true; } $coupons = $this->getValidCoupons($coupons); $transaction = Yii::app()->getDb()->beginTransaction(); try { foreach ($coupons as $coupon) { $model = new OrderCoupon(); $model->setAttributes(['order_id' => $this->id, 'coupon_id' => $coupon->id, 'create_time' => new CDbExpression('NOW()')]); $model->save(); $coupon->decreaseQuantity(); } $this->coupon_discount = $this->getCouponDiscount($coupons); $this->delivery_price = $this->getDeliveryCost(); $this->update(['coupon_discount', 'delivery_price']); $transaction->commit(); return true; } catch (Exception $e) { $transaction->rollback(); return false; } }
public function applyCoupons(array $coupons) { if (!$this->isCouponsAvailable()) { return true; } $coupons = $this->getValidCoupons($coupons); foreach ($coupons as $coupon) { $model = new OrderCoupon(); $model->setAttributes(['order_id' => $this->id, 'coupon_id' => $coupon->id, 'create_time' => new CDbExpression('NOW()')]); $model->save(); if ($this->getIsNewRecord()) { $coupon->decreaseQuantity(); } } $this->coupon_discount = $this->getCouponDiscount($coupons); $this->delivery_price = $this->getDeliveryCost(); return true; }