/** * @param ProductOption $productOption * * @throws \ChingShop\Modules\Sales\Domain\Payment\StockAllocationException * * @return bool */ public function addProductOptionToBasket(ProductOption $productOption) { // Ensure basket saved before associating. $this->basket = $this->getBasket(); $this->saveBasket(); // Check stock. $this->checkStock($productOption); // Add to basket. $basketItem = new BasketItem(); $basketItem->productOption()->associate($productOption); $saved = (bool) $this->basket->basketItems()->save($basketItem); $this->basket()->basketItems->add($basketItem); return $saved; }
/** * Seed an order. */ private function seedOrder() { $basket = Basket::create(); $address = Address::create(['name' => $this->faker()->name, 'line_one' => $this->faker()->streetAddress, 'line_two' => $this->faker()->streetName, 'city' => $this->faker()->city, 'post_code' => $this->faker()->postcode, 'country_code' => $this->faker()->countryCode]); $basket->address()->associate($address); $basket->save(); $basketItems = []; $this->repeat(function () use($basket, &$basketItems) { $basketItems[] = BasketItem::create(['basket_id' => $basket->id, 'product_option_id' => ProductOption::inRandomOrder()->first()->id]); }, random_int(1, 4)); $order = Order::create(); $order->address()->associate($address); $order->save(); /** @var BasketItem $basketItem */ foreach ($basketItems as $basketItem) { $stockItem = new StockItem(); $basketItem->productOption->stockItems()->save($stockItem); $orderItem = OrderItem::create(['basket_item_id' => $basketItem->id, 'order_id' => $order->id, 'price' => $basketItem->priceAsFloat()]); $orderItem->stockItem()->save($stockItem); } Payment::create(['order_id' => $order->id, 'settlement_id' => PayPalSettlement::create(['payment_id' => $this->faker()->uuid, 'payer_id' => $this->faker()->uuid])->id, 'settlement_type' => PayPalSettlement::class]); }
/** * @throws \InvalidArgumentException * * @return Money */ public function linePrice() : Money { return $this->wrappedObject->linePrice(); }
/** * @param BasketItem $basketItem * @param Order $order * * @throws \RuntimeException */ private function basketItemToOrderItem(BasketItem $basketItem, Order $order) { $orderItem = new OrderItem(); $orderItem->price = $basketItem->priceAsFloat(); $orderItem->basketItem()->associate($basketItem); $productOption = $basketItem->productOption; if ($productOption instanceof ProductOptionPresenter) { $productOption = $productOption->getWrappedObject(); } $stockItem = $this->inventory->allocate($productOption); if (!$stockItem->isAvailable()) { throw new StockAllocationException(sprintf('Failed to allocate stock for product option `%s`.', $basketItem->productOption->id)); } $order->orderItems()->save($orderItem); $orderItem->stockItem()->save($stockItem); }