Beispiel #1
0
 public function createOrder(User $user, Seller $seller, Collection $cartItems, Address $address = null, PaymentMethod $paymentMethod = null, $shippingOptions = [])
 {
     if ($address) {
         $address->setFeeType($seller);
     }
     // prepare order item models
     $orderItems = collect([]);
     foreach ($cartItems as $cartItem) {
         $orderItems->push($this->getOrderItemRepository()->getNew(compact('address', 'cartItem') + ['shippingOption' => $shippingOptions[$cartItem->id] ?? null]));
         $product = $cartItem->getProduct();
         $variant = $cartItem->getVariant();
         if (!is_null($product->stock)) {
             $product->stock = $product->stock - $cartItem->quantity;
             $product->save();
         }
         if ($variant and !is_null($variant->stock)) {
             $variant->stock = $variant->stock - $cartItem->quantity;
             $variant->save();
         }
         $this->getCartItemRepository()->delete($cartItem);
     }
     // prepare the order model
     $order = $this->getOrderRepository()->getNew(compact('seller', 'user', 'orderItems', 'address', 'paymentMethod'));
     // associate and persist
     list($saved, $order) = $this->getOrderRepository()->save($order);
     if ($saved) {
         foreach ($orderItems as $orderItem) {
             /** @var OrderItem $orderItem */
             $this->getOrderItemRepository()->save($orderItem->setOrder($order));
         }
     }
     return $order;
 }
 private function checkProductHasNoValidShippingPlanForAddress($product)
 {
     if (!$this->address) {
         return true;
     }
     $this->address->setFeeType($this->seller);
     $shippingPlans = $this->shippingPlanRepository->findWhereProduct($product);
     foreach ($shippingPlans as $shippingPlan) {
         foreach ($shippingPlan->shippingOptions as $option) {
             if (isset($option->{$this->address->getFeeType()}) and $option->{$this->address->getFeeType()}) {
                 return false;
             }
         }
     }
     return true;
 }