/** * @param Request $request * @param ShopContextInterface $context * @return Criteria */ public function createCriteria(Request $request, ShopContextInterface $context) { $criteria = $this->criteriaFactory->createListingCriteria($request, $context); $criteria->removeBaseCondition('category'); $criteria->resetFacets(); $category = $context->getShop()->getCategory()->getId(); $criteria->addBaseCondition(new CategoryCondition([$category])); return $criteria; }
/** * @param string $mode * @param int $category * @return int */ protected function getRandomArticle($mode, $category = 0) { $category = (int) $category; $context = $this->contextService->getShopContext(); if (empty($category)) { $category = $context->getShop()->getCategory()->getId(); } $criteria = $this->storeFrontCriteriaFactory->createBaseCriteria([$category], $context); $criteria->offset(0); switch ($mode) { case 'top': $criteria->addSorting(new PopularitySorting(SortingInterface::SORT_DESC)); $criteria->limit(10); break; case 'new': $criteria->addSorting(new ReleaseDateSorting(SortingInterface::SORT_DESC)); $criteria->limit(1); break; default: $criteria->addSorting(new ReleaseDateSorting(SortingInterface::SORT_DESC)); $criteria->limit(100); } $result = $this->productNumberSearch->search($criteria, $context); $ids = array_map(function (BaseProduct $product) { return $product->getId(); }, $result->getProducts()); $diff = array_diff($ids, $this->cachePromotions); if (empty($diff)) { $diff = $ids; } if ($mode == 'new') { $value = current($diff); } else { shuffle($diff); $value = $diff[array_rand($diff)]; } $this->cachePromotions[] = $value; return $value; }