handleAvailableStock example



    /** * @return EntitySearchResult<ProductCollection> */
    public function load(Criteria $origin, SalesChannelContext $context): EntitySearchResult
    {
        $origin->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
        $criteria = clone $origin;

        $this->addGrouping($criteria);
        $this->handleAvailableStock($criteria$context);

        $ids = $this->productRepository->searchIds($criteria$context);
        /** @var list<string> $keys */
        $keys = $ids->getIds();
        $aggregations = $this->productRepository->aggregate($criteria$context);

        // no products found, no need to continue         if (empty($keys)) {
            return new EntitySearchResult(
                ProductDefinition::ENTITY_NAME,
                0,
                
$filters = $this->productStreamBuilder->buildFilters(
            $productStreamId,
            $context->getContext()
        );

        $criteria->addFilter(...$filters)
            ->setOffset(0)
            ->setLimit($crossSelling->getLimit())
            ->addSorting($crossSelling->getSorting());

        $criteria = $this->handleAvailableStock($criteria$context);

        $this->eventDispatcher->dispatch(
            new ProductCrossSellingStreamCriteriaEvent($crossSelling$criteria$context)
        );

        $searchResult = $this->listingLoader->load($criteria$context);

        /** @var ProductCollection $products */
        $products = $searchResult->getEntities();

        $element = new CrossSellingElement();
        
new EqualsFilter('wishlists.wishlistId', $wishlistId)
        );

        $criteria->addSorting(
            new FieldSorting('wishlists.updatedAt', FieldSorting::DESCENDING)
        );

        $criteria->addSorting(
            new FieldSorting('wishlists.createdAt', FieldSorting::DESCENDING)
        );

        $criteria = $this->handleAvailableStock($criteria$context);

        $event = new CustomerWishlistLoaderCriteriaEvent($criteria$context);
        $this->eventDispatcher->dispatch($event);

        $products = $this->productRepository->search($criteria$context);

        $event = new CustomerWishlistProductListingResultEvent($request$products$context);
        $this->eventDispatcher->dispatch($event);

        return $products;
    }

    
Home | Imprint | This part of the site doesn't use cookies.