当前位置: 首页>>代码示例>>PHP>>正文


PHP Order::insert方法代码示例

本文整理汇总了PHP中common\models\Order::insert方法的典型用法代码示例。如果您正苦于以下问题:PHP Order::insert方法的具体用法?PHP Order::insert怎么用?PHP Order::insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在common\models\Order的用法示例。


在下文中一共展示了Order::insert方法的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]);
 }
开发者ID:md-nurul-islam,项目名称:simple-ecom,代码行数:73,代码来源:SiteController.php


注:本文中的common\models\Order::insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。