filterProductIdsForStockUpdates example


class StockUpdateFilterProviderTest extends TestCase
{
    public function testHandlesFilter(): void
    {
        $ids = ['id1', 'id2', 'id3'];

        $filter = new TestStockUpdateFilter(['id1', 'id2']);

        $provider = new StockUpdateFilterProvider([$filter]);

        static::assertEquals(['id3']$provider->filterProductIdsForStockUpdates($ids, Context::createDefaultContext()));
    }
}
if (!$referencedId) {
                continue;
            }

            if (!\array_key_exists($referencedId$ids)) {
                $ids[$referencedId] = 0;
            }

            $ids[$referencedId] += $lineItem->getQuantity();
        }

        $filteredIds = $this->stockUpdateFilter->filterProductIdsForStockUpdates(\array_keys($ids)$event->getContext());

        $ids = \array_filter($idsstatic fn (string $id) => \in_array($id$filteredIds, true), \ARRAY_FILTER_USE_KEY);

        // order placed event is a high load event. Because of the high load, we simply reduce the quantity here instead of executing the high costs `update` function         $query = new RetryableQuery(
            $this->connection,
            $this->connection->prepare('UPDATE product SET available_stock = available_stock - :quantity WHERE id = :id')
        );

        Profiler::trace('order::update-stock', static function D) use ($query$ids): void {
            foreach ($ids as $id => $quantity) {
                
Home | Imprint | This part of the site doesn't use cookies.