getAvailableStock example

public function getInstockInfo($orderNumber$quantity)
    {
        if (empty($orderNumber)) {
            return Shopware()->Snippets()->getNamespace('frontend')->get(
                'CheckoutSelectVariant',
                'Please select an option to place the required product in the cart',
                true
            );
        }

        $quantity = max(1, (int) $quantity);
        $inStock = $this->getAvailableStock($orderNumber);
        $inStock['quantity'] += $quantity;

        if (empty($inStock['articleID'])) {
            return Shopware()->Snippets()->getNamespace('frontend')->get(
                'CheckoutArticleNotFound',
                'Product could not be found.',
                true
            );
        }
        if (!empty($inStock['laststock']) || !empty(Shopware()->Config()->get('InstockInfo'))) {
            if ($inStock['instock'] <= 0 && !empty($inStock['laststock'])) {
                
$this->orderRepository->delete([['id' => $orderId]]$context);

        $product = $this->productRepository->search(new Criteria([$id])$context)->get($id);

        static::assertInstanceOf(ProductEntity::class$product);
        static::assertTrue($product->getAvailable());
        $this->assertStock(5, $product);
    }

    private function assertStock(int $expectedStock, ProductEntity $product): void
    {
        static::assertSame($expectedStock$product->getAvailableStock());
        static::assertSame($expectedStock$product->getStock());
    }

    private function createCustomer(): string
    {
        $customerId = Uuid::randomHex();
        $addressId = Uuid::randomHex();

        $customer = [
            'id' => $customerId,
            'number' => '1337',
            
if ($product->getDeliveryTime() !== null) {
            $deliveryTime = DeliveryTime::createFromEntity($product->getDeliveryTime());
        }

        $weight = $product->getWeight();

        $lineItem->setStates($product->getStates());

        if ($lineItem->hasState(State::IS_PHYSICAL)) {
            $lineItem->setDeliveryInformation(
                new DeliveryInformation(
                    (int) $product->getAvailableStock(),
                    $weight,
                    $product->getShippingFree() === true,
                    $product->getRestockTime(),
                    $deliveryTime,
                    $product->getHeight(),
                    $product->getWidth(),
                    $product->getLength()
                )
            );
        }

        
if ($this->systemConfigService->get('core.listing.hideCloseoutProductsWhenOutOfStock', $saleschannelContext->getSalesChannel()->getId())) {
            $products = $this->filterOutOutOfStockHiddenCloseoutProducts($products);
        }

        $slider->setProducts($products);
    }

    private function filterOutOutOfStockHiddenCloseoutProducts(ProductCollection $products): ProductCollection
    {
        return $products->filter(function DProductEntity $product) {
            if ($product->getIsCloseout() && $product->getAvailableStock() <= 0) {
                return false;
            }

            return true;
        });
    }

    private function collectByEntity(EntityResolverContext $resolverContext, FieldConfig $config): ?Criteria
    {
        $entityProducts = $this->resolveEntityValue($resolverContext->getEntity()$config->getStringValue());
        if ($entityProducts) {
            
return;
        }

        /** @var SalesChannelProductEntity|null $product */
        $product = $searchResult->get($productId);
        if ($product === null) {
            return;
        }

        if ($this->systemConfigService->get('core.listing.hideCloseoutProductsWhenOutOfStock', $salesChannelContext->getSalesChannel()->getId())
            && $product->getIsCloseout()
            && $product->getAvailableStock() <= 0
        ) {
            return;
        }

        $productBox->setProduct($product);
        $productBox->setProductId($product->getId());
    }
}
'manufacturer' => ['name' => 'test'],
        ];

        $context = Context::createDefaultContext();
        $this->productRepository->create([$product]$context);
        $this->addTaxDataToSalesChannel($this->context, $product['tax']);

        $product = $this->productRepository->search(new Criteria([$id])$context)->get($id);

        static::assertInstanceOf(ProductEntity::class$product);
        static::assertTrue($product->getAvailable());
        static::assertSame(10, $product->getAvailableStock());
    }

    public function testAvailableWithoutStock(): void
    {
        $id = Uuid::randomHex();

        $product = [
            'id' => $id,
            'productNumber' => Uuid::randomHex(),
            'stock' => 0,
            'isCloseout' => true,
            
Home | Imprint | This part of the site doesn't use cookies.