示例#1
0
 /**
  * Attach a user's shipping address to their order
  * 
  * @param   Order   $order
  * @param   array   $address
  * 
  * @return  Order
  */
 private function attachShippingAddress(Order $order, array $address)
 {
     // Make sure all required keys are present
     if (!array_key_exists('first_name', $address) || empty($address['first_name']) || !array_key_exists('last_name', $address) || empty($address['last_name']) || !array_key_exists('email', $address) || empty($address['email']) || !array_key_exists('address1', $address) || empty($address['address1']) || !array_key_exists('address2', $address) || !array_key_exists('city', $address) || empty($address['city']) || !array_key_exists('state', $address) || empty($address['state']) || !array_key_exists('postcode', $address) || empty($address['postcode']) || !array_key_exists('country', $address) || empty($address['country'])) {
         throw new Exception('Missing required shipping information.');
     }
     // Address cleaning
     foreach ($address as $key => $value) {
         $address[$key] = trim($value);
     }
     $address['first_name'] = ucfirst(strtolower($address['first_name']));
     $address['last_name'] = ucfirst(strtolower($address['last_name']));
     // Make the customer
     $customer = Customer::firstOrCreate(['first_name' => $address['first_name'], 'last_name' => $address['last_name'], 'email' => $address['email']]);
     // Attach the customer to the order
     $order->shipping_address = $address;
     $order->customer_id = $customer->id;
     $order->save();
     return $order;
 }
示例#2
0
文件: Cart.php 项目: janusnic/shop
 /**
  * Completes a shopping cart
  * @param   Order   $order
  */
 public function markAsComplete(Order $order)
 {
     $this->load('items.inventory');
     foreach ($this->items as $item) {
         $item->inventory->quantity -= $item->quantity;
         $item->inventory->save();
     }
     $order->is_complete = true;
     $order->save();
     if (!$this->couponIsApplied || !isset($this->coupon->name)) {
         $this->coupon_id = null;
     } else {
         $this->backup_couponName = $this->coupon->name;
     }
     $this->backup_total = $this->total;
     $this->backup_totalBeforecoupon = $this->totalBeforeCoupon;
     $this->backup_fullTotal = $this->fullTotal;
     $this->order_id = $order->id;
     $this->save();
     Session::forget('bedard_shop_order');
 }