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; }
/** * @param Address $address * @return \Illuminate\Http\RedirectResponse */ public function primarize(Address $address) { if (!$address->isPrimary()) { DB::transaction(function () use($address) { $this->addressManager->primarizeAddress($address, $this->user()); }); } return $this->success('edit'); }
/** * @param $address * @param null $owner * @return mixed */ public function primarizeAddress(Address $address, $owner = null) { $repository = $this->getAddressRepository(); if (!$owner) { $owner = $address->getOwner(); } /** @var Address $primaryAddress */ if ($primaryAddress = $repository->whereOwner($owner)->scopes('primary')->first()) { $primaryAddress->setPrimary(false); $repository->save($primaryAddress); } list($updated, $address) = $repository->save($address->setPrimary()); return $address; }
private function getNewOrderItem(Address $address = null, CartItem $cartItem, ShippingOption $shippingOption = null) { $product = $cartItem->getProduct(); $feeType = $address ? $address->getFeeType() : null; /** @var \App\Modules\OrderModule\Entities\OrderItem $orderItem */ $orderItem = $this->orderItemRepository->createModel(['product_name' => $product->name, 'price' => $product->price, 'sale_price' => $product->sale_price, 'products_total' => $cartItem->products_total ?? 0, 'shipping_total' => $shippingOption && $feeType ? $shippingOption->{$feeType} * $cartItem->quantity : 0, 'quantity' => $cartItem->quantity]); $orderItem->setAttributes($cartItem->getAttributeOptions()); if ($shippingOption && $address) { $orderItem->setShiping($shippingOption->snapshot($feeType, $cartItem->quantity)); } $orderItem->setProduct($cartItem->getProduct()); $this->cartItemRepository->delete($cartItem); return $orderItem; }
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; }
public function setAddress(Address $address) { $this->attributes['address'] = json_encode($address->snapshot()); }