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