common\models\Order::cancel PHP Method

cancel() public method

public cancel ( $msg = null )
    public function cancel($msg = null)
    {
        $this->beforeCancel();
        $transaction = Yii::$app->db->beginTransaction();
        try {
            $this->status = self::STATUS_CANCELLED;
            $this->cancelled_msg = $msg;
            if (!$this->save(false)) {
                throw new \Exception('订单错误!');
            }
            foreach ($this->goods as $goods) {
                if (!$goods->goods->moveSurplus(+$goods->count, "取消订单:{$this->order_sn}。")) {
                    throw new \Exception('订单商品错误!');
                }
            }
            $transaction->commit();
            $this->afterCancel();
            return true;
        } catch (\Exception $e) {
            $transaction->rollBack();
            return false;
        }
    }

Usage Example

 public function cancel($runValidation = true)
 {
     if ($runValidation && !$this->validate()) {
         return false;
     }
     if ($this->_order->status !== Order::STATUS_UNSHIPPED) {
         return false;
     }
     if ($this->_order->payment !== Order::PAYMENT_OFFLINE) {
         return false;
     }
     return $this->_order->cancel($this->msg);
 }