本文整理匯總了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]);
}