getStockForProductId example



        $result = new AvailableCombinationResult();
        foreach ($combinations as $id => $combination) {
            try {
                $options = json_decode((string) $combination['options'], true, 512, \JSON_THROW_ON_ERROR);
            } catch (\JsonException) {
                continue;
            }

            $available = (bool) $combination['available'];
            $stockData = $stocks->getStockForProductId($id);

            if ($stockData !== null) {
                $available = $stockData->available;
            }

            $result->addCombination($options$available);
        }

        return $result;
    }

    


    public function salesChannelLoaded(SalesChannelEntityLoadedEvent $event): void
    {
        $stocks = $this->stockStorage->load(
            new StockLoadRequest($event->getIds()),
            $event->getSalesChannelContext()
        );

        foreach ($event->getEntities() as $product) {
            /** @var ProductEntity $product */
            $stock = $stocks->getStockForProductId($product->getId());

            if ($stock === null) {
                continue;
            }

            $product->assign([
                // required stock data                 'stock' => $stock->stock,
                'available' => $stock->available,
                // optional stock data                 'minPurchase' => $stock->minPurchase ?? $product->get('minPurchase'),
                
public function testEmptyCollection(): void
    {
        $collection = new StockDataCollection([]);

        static::assertEmpty($collection->all());
    }

    public function testGetStockForProductId(): void
    {
        $collection = new StockDataCollection([]);

        static::assertNull($collection->getStockForProductId('12345'));

        $stock1 = new StockData('12345', 10, true);
        $collection = new StockDataCollection([
            $stock1,
        ]);

        static::assertSame($stock1$collection->getStockForProductId('12345'));
        static::assertNull($collection->getStockForProductId('23456'));
    }

    public function testAdd(): void
    {
Home | Imprint | This part of the site doesn't use cookies.