public function create()
 {
     if (!isset($_POST['quantity']) || !isset($_POST['product_id']) || !isset($_POST['product_price'])) {
         return call('pages', 'error');
     }
     if (isAdmin()) {
         $_SESSION['alert'] = "Admin is not able to buy products";
         return header("Location: index.php?controller=products&action=index");
     }
     if (!isset($_SESSION['id'])) {
         $_SESSION['alert'] = "Please log in before shopping";
         return header("Location: index.php?controller=products&action=index");
     }
     if (!Order::isValid($_SESSION['id'])) {
         $_SESSION['alert'] = "Before you can buy products, you must provide necessary perfonal information";
         return header("Location: index.php?controller=products&action=index");
     }
     if (!isset($_SESSION['orderID'])) {
         $_SESSION['orderID'] = Order::create($_SESSION['id']);
     }
     require_once 'models/order_detail.php';
     if (OrderDetail::check($_SESSION['orderID'], $_POST['product_id'])) {
         OrderDetail::addQuantity($_SESSION['orderID'], $_POST['product_id'], $_POST['quantity']);
     } else {
         OrderDetail::create($_SESSION['orderID'], $_POST['product_id'], $_POST['product_price'], $_POST['quantity']);
     }
     $_SESSION['notice'] = "Added product to basket";
     header("Location: index.php?controller=products&action=index");
 }
Exemple #2
0
 /**
  * postCheckOut
  */
 public function postCheckOut()
 {
     $customer = Input::get('customer');
     $customer = Customer::find($customer);
     $customer_address = CustomerReceivedAddress::where('customer_id', $customer->id)->where('default', 1)->first();
     //订单编号
     $orderno = OrderGeneration::where('customer_no', $customer->customer_no)->where('used', 0)->first();
     if (!$orderno) {
         foreach (range(1001, 9998) as $index) {
             if ($index == 1111 || $index == 2222 || $index == 3333 || $index == 4444 || $index == 5555 || $index == 6666 || $index == 7777 || $index == 8888) {
                 continue;
             }
             OrderGeneration::create(['customer_no' => $customer->customer_no, 'order_no' => $index, 'used' => 0]);
         }
         $order_no = $index;
         $no = $order_no . $customer->customer_no;
     } else {
         $order_no = $orderno->order_no;
         $no = $order_no . $customer->customer_no;
     }
     $order = new Order();
     $order->no = $no;
     $order->customer_id = $customer->id;
     $order->customer_address_id = $customer_address->id;
     $order->payment_method_id = 1;
     $order->shipping_method_id = 1;
     $order->order_status = 'order_price_confirmed';
     $order->item_fee = 0.0;
     $order->shipping_fee = 0;
     $order->amount_fee = 0.0;
     $order->point_fee = 0;
     $order->credit_fee = 0;
     $order->pay_fee = 0.0;
     $order->caution_money = 0;
     $order->save();
     $ordergeneration = OrderGeneration::where('order_no', $order_no)->where('customer_no', $customer->customer_no)->first();
     $ordergeneration->used = 1;
     $ordergeneration->save();
     //orderdetail
     $carts = Cart::where('customer_id', $customer->id)->get();
     $sum = 0;
     foreach ($carts as $cart) {
         $total = 0;
         if ($cart->confirm_price == 0.0) {
             $cart->confirm_price = $cart->item->price_market;
         }
         OrderDetail::create(['order_id' => $order->id, 'item_id' => $cart->item_id, 'craft_description' => $cart->craft_description, 'quantity' => $cart->quantity, 'confirm_price' => $cart->confirm_price, 'confirm_quantity' => 0, 'delivery_quantity' => 0]);
         $total = $cart->confirm_price * $cart->quantity;
         $sum += $total;
     }
     $order = Order::find($order->id);
     $order->item_fee = $sum;
     $order->save();
     //查看订单详细页面
     return Redirect::to('admin/orders');
 }
 public function parseOrderDetails($orders, $transaction)
 {
     // remove previous OrderDetails so we don't end up with duplicates
     foreach ($transaction->Details() as $detail) {
         $detail->delete();
     }
     foreach ($orders->transactions->transaction as $order) {
         // Associate ProductPages, Options, Quanity with Order
         foreach ($order->transaction_details->transaction_detail as $product) {
             $OrderDetail = OrderDetail::create();
             // set Quantity
             $OrderDetail->Quantity = (int) $product->product_quantity;
             // set calculated price (after option modifiers)
             $OrderDetail->Price = (double) $product->product_price;
             // Find product via product_id custom variable
             foreach ($product->transaction_detail_options->transaction_detail_option as $productID) {
                 if ($productID->product_option_name == 'product_id') {
                     $OrderProduct = ProductPage::get()->filter('ID', (int) $productID->product_option_value)->First();
                     // if product could be found, then set Option Items
                     if ($OrderProduct) {
                         // set ProductID
                         $OrderDetail->ProductID = $OrderProduct->ID;
                         // loop through all Product Options
                         foreach ($product->transaction_detail_options->transaction_detail_option as $option) {
                             $OptionItem = OptionItem::get()->filter(array('ProductID' => (string) $OrderProduct->ID, 'Title' => (string) $option->product_option_value))->First();
                             if ($OptionItem) {
                                 $OrderDetail->Options()->add($OptionItem);
                                 // modify product price
                                 if ($priceMod = $option->price_mod) {
                                     $OrderDetail->Price += $priceMod;
                                 }
                             }
                         }
                     }
                 }
                 // associate with this order
                 $OrderDetail->OrderID = $transaction->ID;
                 // extend OrderDetail parsing, allowing for recording custom fields from FoxyCart
                 $this->extend('handleOrderItem', $decrypted, $product, $OrderDetail);
                 // write
                 $OrderDetail->write();
             }
         }
     }
 }
 function createOrder()
 {
     $user_id = 0;
     if (Auth::user()->check()) {
         $user_id = Auth::user()->get()->id;
     }
     //Add billing address
     $billing_address = array();
     if (Session::has('billing_address')) {
         $billing_address = Session::get('billing_address');
         $billing_address['user_id'] = $user_id;
         $billing_address['country_name'] = Address::countryName($billing_address['country_a2']);
         if (!empty($billing_address['state_a2'])) {
             $billing_address['state_name'] = Address::stateName($billing_address['state_a2'], $billing_address['country_a2']);
         }
         $billing_address['is_billing'] = 1;
         $obj_billing_address = $this->createAddress($billing_address);
     }
     //add shipping address
     $shipping_address = array();
     if (Session::has('shipping_address')) {
         $shipping_address = Session::get('shipping_address');
         $shipping_address['user_id'] = $user_id;
         $shipping_address['country_name'] = Address::countryName($shipping_address['country_a2']);
         if (!empty($shipping_address['state_a2'])) {
             $shipping_address['state_name'] = Address::stateName($shipping_address['state_a2'], $shipping_address['country_a2']);
         }
         $shipping_address['is_billing'] = 0;
         $obj_shipping_address = $this->createAddress($shipping_address);
     }
     if (isset($obj_billing_address) && isset($obj_shipping_address)) {
         //Add to order table
         $cart_total = Cart::total();
         $order = Order::create(["user_id" => $user_id, "sum_amount" => $cart_total, "billing_address_id" => $obj_billing_address->id, "shipping_address_id" => $obj_shipping_address->id, "status" => "New"]);
         //Add to order_details
         if ($order) {
             $cart_content = Cart::content();
             foreach ($cart_content as $item) {
                 $options = [];
                 foreach ($item->options->options as $option) {
                     $options[] = $option['key'];
                 }
                 $option_keys = implode(",", $options);
                 $orderDetail = OrderDetail::create(["order_id" => $order->id, "image_id" => $item->id, "quantity" => $item->qty, "sell_price" => $item->price, "sum_amount" => $item->subtotal, "type" => $item->options->order_type, "size" => $item->options->size, "option" => $option_keys]);
             }
             //Clear session
             Cart::destroy();
             Session::forget('billing_address');
             Session::forget('shipping_address');
             return $order;
         }
     }
     return false;
 }