本文整理汇总了PHP中common\models\Product::updateAllCounters方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::updateAllCounters方法的具体用法?PHP Product::updateAllCounters怎么用?PHP Product::updateAllCounters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类common\models\Product
的用法示例。
在下文中一共展示了Product::updateAllCounters方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionAjaxStatus
public function actionAjaxStatus($id, $status)
{
Yii::$app->response->format = Response::FORMAT_JSON;
$model = $this->findModel($id);
if ($model) {
$oldStatus = $model->status;
$model->status = $status;
$model->save();
// 记录订单日志
$orderLog = new OrderLog(['order_id' => $model->id, 'status' => $model->status]);
$orderLog->save();
// 如果订单为取消,则恢复对应的库存
if ($oldStatus > 0 && $status == Order::STATUS_CANCEL) {
$orderProducts = OrderProduct::find()->where(['order_id' => $model->id])->all();
foreach ($orderProducts as $product) {
Product::updateAllCounters(['stock' => $product->number], ['id' => $product->product_id]);
}
}
return ['status' => 1];
}
return ['status' => -1];
}
示例2: actionCheckout
public function actionCheckout()
{
Yii::$app->session['step'] = 2;
$userId = Yii::$app->user->id;
$addresses = Address::find()->where(['user_id' => $userId])->all();
$model = new Order();
if ($model->load(Yii::$app->request->post())) {
if (!Yii::$app->request->post('address_id')) {
return $this->goBack();
}
// 使用优惠券
$feeCouponUser = $feeCouponCode = $feePoint = 0.0;
$couponId = Yii::$app->request->post('coupon');
if ($couponId && Yii::$app->request->post('checkbox-coupon')) {
$couponUser = Coupon::findOne($couponId);
if (!$couponUser || $couponUser->used_at > 0 || $couponUser->ended_at < time() || $couponUser->user_id != Yii::$app->user->id) {
return $this->goBack();
}
$feeCouponUser = $couponUser->money;
}
// 使用优惠码
$sn = Yii::$app->request->post('sn');
if ($sn) {
$couponCode = Coupon::find()->where(['sn' => $sn])->one();
if (!$couponCode || $couponCode->used_at > 0 || $couponCode->ended_at < time()) {
return $this->goBack();
}
$feeCouponCode = $couponCode->money;
}
// 使用积分
$point = Yii::$app->request->post('point');
if ($point && Yii::$app->request->post('checkbox-point')) {
if ($point > Yii::$app->user->identity->point) {
return $this->goBack();
}
$feePoint = intval($point) / 100;
}
$address = Address::find()->where(['id' => Yii::$app->request->post('address_id'), 'user_id' => $userId])->one();
$model->user_id = $userId;
$model->sn = date('YmdHis') . rand(1000, 9999);
$model->consignee = $address->consignee;
$model->country = $address->country;
$model->province = $address->province;
$model->city = $address->city;
$model->district = $address->district;
$model->address = $address->address;
$model->zipcode = $address->zipcode;
$model->phone = $address->phone;
$model->mobile = $address->mobile;
$model->email = $address->email ? $address->email : Yii::$app->user->identity->email;
if ($model->payment_method == Order::PAYMENT_METHOD_COD) {
$model->payment_status = Order::PAYMENT_STATUS_COD;
} else {
$model->payment_status = Order::PAYMENT_STATUS_UNPAID;
}
$model->status = $model->payment_status;
$products = Cart::find()->where(['session_id' => Yii::$app->session->id])->all();
if (count($products)) {
foreach ($products as $product) {
$model->amount += $product->number * $product->price;
}
} else {
$this->redirect('/cart');
}
$model->amount += floatval($model->shipment_fee) - $feeCouponUser - $feeCouponCode - $feePoint;
if ($model->save()) {
// insert order_product and clear cart
foreach ($products as $product) {
$orderProduct = new OrderProduct();
$orderProduct->order_id = $model->id;
$orderProduct->product_id = $product->product_id;
$orderProduct->sku = $product->sku;
$orderProduct->name = $product->name;
$orderProduct->number = $product->number;
$orderProduct->market_price = $product->market_price;
$orderProduct->price = $product->price;
$orderProduct->thumb = $product->thumb;
$orderProduct->type = $product->type;
$orderProduct->save();
// 减少商品的库存
Product::updateAllCounters(['stock' => -$product->number], ['id' => $product->product_id]);
}
// 生成订单后,清空购物车,设置优惠码,更新积分和积分记录
Cart::deleteAll(['session_id' => Yii::$app->session->id]);
if (isset($couponUser) && Yii::$app->request->post('checkbox-coupon')) {
$couponUser->used_at = time();
$couponUser->order_id = $model->id;
$couponUser->save();
}
if (isset($couponCode) && Yii::$app->request->post('checkbox-coupon')) {
$couponCode->user_id = Yii::$app->user->id;
$couponCode->used_at = time();
$couponCode->order_id = $model->id;
$couponCode->save();
}
if (isset($point) && Yii::$app->request->post('checkbox-point')) {
$balance = Yii::$app->user->identity->point - $point;
User::updateAllCounters(['point' => -$point], ['id' => Yii::$app->user->id]);
$pointLog = new PointLog(['user_id' => Yii::$app->user->id, 'type' => PointLog::POINT_TYPE_BUYING, 'point' => -$point, 'balance' => $balance]);
$pointLog->save();
//.........这里部分代码省略.........