ManualSorting example

if (!isset($sorting['position'])) {
                throw new CustomValidationException(sprintf('Field position is missing in manualSorting array'));
            }

            if (!$connection->fetchOne('SELECT 1 FROM s_articles_categories_ro WHERE categoryID = ? AND articleID = ?', [
                $category->getId(),
                $sorting['product_id'],
            ])) {
                throw new CustomValidationException(sprintf('Product with id %d is not assigned to the category', $sorting['product_id']));
            }

            $sortingObj = new ManualSorting();
            $sortingObj->setCategory($category);

            $product = $this->getManager()->find(Product::class$sorting['product_id']);
            if (!$product instanceof Product) {
                throw new ModelNotFoundException(Product::class$sorting['product_id']);
            }

            $sortingObj->setProduct($product);
            $sortingObj->setPosition((int) $sorting['position']);

            $collection[] = $sortingObj;
        }
MediaServiceInterface $mediaService
    ) {
        $this->queryBuilderFactory = $queryBuilderFactory;
        $this->contextService = $contextService;
        $this->mediaService = $mediaService;
    }

    public function load(int $categoryId, ?int $start, ?int $limit, CustomSorting $customSorting): array
    {
        $criteria = new Criteria();
        $criteria->addBaseCondition(new CategoryCondition([$categoryId]));
        $criteria->addSorting(new ManualSorting());
        $criteria->offset($start);
        $criteria->limit($limit);

        foreach ($customSorting->getSortings() as $sorting) {
            $criteria->addSorting($sorting);
        }

        $query = $this->queryBuilderFactory->createQueryWithSorting($criteria$this->contextService->getShopContext());

        $data = [];
        $query
            
Home | Imprint | This part of the site doesn't use cookies.