/**
  * @param SortingInterface $sorting
  * @param QueryBuilder $query
  * @param ShopContextInterface $context
  * @throws \Exception
  */
 public function generateSorting(SortingInterface $sorting, QueryBuilder $query, ShopContextInterface $context)
 {
     /** @var Listing $categoryComponent */
     $categoryComponent = Shopware()->Container()->get('swagcustomsort.listing_component');
     $categoryId = Shopware()->Front()->Request()->getParam('sCategory');
     $linkedCategoryId = $categoryComponent->getLinkedCategoryId($categoryId);
     $hasCustomSort = $categoryComponent->hasCustomSort($categoryId);
     $baseSort = $categoryComponent->getCategoryBaseSort($categoryId);
     if ($hasCustomSort || $baseSort > 0) {
         $baseSorting = $categoryComponent->getCategoryBaseSort($categoryId);
     } else {
         $baseSorting = Shopware()->Config()->get('defaultListingSorting');
     }
     //apply 'plugin' order
     if ($linkedCategoryId) {
         $query->leftJoin('productCategory', 's_articles_sort', 'customSort', 'customSort.articleId = productCategory.articleID AND (customSort.categoryId = :sortCategoryId OR customSort.categoryId IS NULL)');
         $query->setParameter('sortCategoryId', $linkedCategoryId);
     } else {
         $query->leftJoin('productCategory', 's_articles_sort', 'customSort', 'customSort.articleId = productCategory.articleID AND (customSort.categoryId = productCategory.categoryID OR customSort.categoryId IS NULL)');
     }
     //exclude passed products ids from result
     $sortedProductsIds = $this->sortingComponent->getSortedProductsIds();
     if ($sortedProductsIds) {
         $query->andWhere($query->expr()->notIn("product.id", $sortedProductsIds));
     }
     //for records with no 'plugin' order data use the default shopware order
     $handlerData = $this->getDefaultData($baseSorting);
     if ($handlerData) {
         $sorting->setDirection($handlerData['direction']);
         $handlerData['handler']->generateSorting($sorting, $query, $context);
     }
 }
 /**
  * Creates a search request on the internal search gateway to
  * get the product result for the passed criteria object.
  *
  * @param Criteria $criteria
  * @param Struct\ProductContextInterface $context
  * @return ProductSearchResult
  */
 public function search(Criteria $criteria, Struct\ProductContextInterface $context)
 {
     $productSearchResult = $this->productSearch->search($criteria, $context);
     $facets = $productSearchResult->getFacets();
     $totalCount = $productSearchResult->getTotalCount() + $this->sortingComponent->getTotalCount();
     return new ProductNumberSearchResult($productSearchResult->getProducts(), $totalCount, $facets);
 }
Exemple #3
0
 /**
  * When a Criteria is created, check for plugin sorting options. If plugin sorting options exist, add them.
  *
  * @param Enlight_Event_EventArgs $args
  */
 public function onCreateListingCriteria(Enlight_Event_EventArgs $args)
 {
     /** @var Request $request */
     $request = $args->get('request');
     /** @var Criteria $criteria */
     $criteria = $args->get('criteria');
     $allowedActions = ['index', 'ajaxListing', 'productNavigation'];
     //Don't apply custom sort if we are not in category listing
     if (!in_array($request->getActionName(), $allowedActions)) {
         return;
     }
     if (!$this->listingComponent instanceof Listing) {
         return;
     }
     $categoryId = (int) $request->getParam('sCategory');
     $useDefaultSort = $this->listingComponent->showCustomSortAsDefault($categoryId);
     $sortName = $this->listingComponent->getFormattedSortName();
     $baseSort = $this->listingComponent->getCategoryBaseSort($categoryId);
     $sortId = $request->getParam('sSort');
     if ($request->getParam('sSort') == SortFactory::DRAG_DROP_SORTING) {
         $useDefaultSort = true;
     }
     if (!$useDefaultSort && $baseSort || empty($sortName) || $sortId !== null && $sortId != SortFactory::DRAG_DROP_SORTING) {
         return;
     }
     $criteria->resetSorting();
     $request->setParam('sSort', SortFactory::DRAG_DROP_SORTING);
     $page = (int) $request->getParam('sPage');
     $offset = (int) $criteria->getOffset();
     $limit = (int) $criteria->getLimit();
     //Get all sorted products for current category and set them in components for further sorting
     $linkedCategoryId = $this->listingComponent->getLinkedCategoryId($categoryId);
     $sortedProducts = $this->em->getRepository('\\Shopware\\CustomModels\\CustomSort\\ArticleSort')->getSortedProducts($categoryId, $linkedCategoryId);
     $this->sortingComponent->setSortedProducts($sortedProducts);
     //Get new offset based on page so we can get correct position of unsorted products
     $newOffset = $this->sortingComponent->getOffset($offset, $page, $limit);
     $this->sortingComponent->setOffsetAndLimit($offset, $limit);
     $criteria->offset($newOffset);
     $sorter = new SortFactory($request, $criteria);
     $sorter->addSort();
 }
 /**
  * @param array $numbers
  * @param Struct\ProductContextInterface $context
  * @return Struct\ListProduct[]
  */
 public function getList(array $numbers, Struct\ProductContextInterface $context)
 {
     $getSortedNumbers = $this->sortingComponent->sortByNumber($numbers);
     $products = $this->coreService->getList($getSortedNumbers, $context);
     return $products;
 }