本文整理汇总了PHP中common\models\Order::beforeSave方法的典型用法代码示例。如果您正苦于以下问题:PHP Order::beforeSave方法的具体用法?PHP Order::beforeSave怎么用?PHP Order::beforeSave使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类common\models\Order
的用法示例。
在下文中一共展示了Order::beforeSave方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionCheckout
public function actionCheckout()
{
$user_id = 0;
if (Yii::$app->user->isGuest) {
return $this->redirect('/login');
}
if (!Yii::$app->user->isGuest) {
$user_id = Yii::$app->user->identity->id;
}
$cart_json = [];
if (isset($_COOKIE["cart_{$user_id}"])) {
$cart_json = Json::decode($_COOKIE["cart_{$user_id}"]);
} elseif (isset($_COOKIE["cart_0"])) {
$cart_json = Json::decode($_COOKIE["cart_0"]);
} else {
return $this->redirect('/shop');
Yii::$app->end();
}
$ar_product_id = array_keys($cart_json);
$cart_items = Product::find()->where(['in', 'id', $cart_json])->all();
if (isset($_POST) && !empty($_POST['place_order'])) {
$grand_total = 0.0;
$grand_total_purchase_price = 0.0;
foreach ($cart_items as $ci) {
$unit_price = floatval($ci->selling_price);
$qty = intval($cart_json[$ci->id]['qty']);
$row_total = $unit_price * $qty;
$grand_total += $row_total;
$unit_purchase_price = floatval($ci->purchase_price);
$row_total_purchase_price = $unit_purchase_price * $qty;
$grand_total_purchase_price += $row_total_purchase_price;
}
$order = new Order();
$order->bill_number = Custom::getUniqueId(0, 6);
$order->member_id = $user_id;
$order->total_amount = $grand_total;
$order->total_payable = $grand_total;
$order->payment_method = $_POST['payment_method'];
$order->beforeSave(true);
if ($order->validate() && $order->insert()) {
$product = '';
foreach ($cart_items as $ci) {
$unit_price = floatval($ci->selling_price);
$qty = intval($cart_json[$ci->id]['qty']);
$row_total = $unit_price * $qty;
$product .= "{$ci->id},";
$cart = new Cart();
$cart->order_id = $order->id;
$cart->product_id = $ci->id;
$cart->unit_selling_price = $ci->selling_price;
$cart->quantity_sold = $qty;
$cart->subtotal_payable = $row_total;
$cart->subtotal_paid = $row_total;
$cart->beforeSave(true);
$cart->insert();
}
}
$sale_amount = $grand_total - $grand_total_purchase_price;
$app_root = \Yii::getAlias('@approot');
include "{$app_root}/affiliate/controller/record-sale.php";
$cookies = Yii::$app->response->cookies;
if (isset($_COOKIE['cart_' . $user_id])) {
$cookies->remove('cart_' . $user_id);
unset($cookies['cart_' . $user_id]);
}
if (isset($_COOKIE['cart_0'])) {
$cookies->remove('cart_0');
unset($cookies['cart_0']);
}
return $this->redirect(['/member/order', 'id' => $order->id]);
}
return $this->render('checkout', ['cart_items' => $cart_items, 'cookie_data' => $cart_json]);
}