/** * 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(); }