Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.26 KB | None | 0 0
  1. <?php
  2.  
  3. namespace VossMedienJvp;
  4.  
  5. use Shopware\Bundle\SearchBundle\Condition\CategoryCondition;
  6. use Shopware\Bundle\SearchBundle\Criteria;
  7. use Shopware\Components\Plugin;
  8. use VossMedienJvp\SearchBundle\ManufacturerConditionHandler;
  9. use VossMedienJvp\SearchBundle\ManufacturerFacetHandler;
  10.  
  11. class VossMedienJvp extends Plugin
  12. {
  13.  
  14.     public static function getSubscribedEvents()
  15.     {
  16.         return [
  17.             'Enlight_Controller_Action_PostDispatch_Frontend' => 'onPostDispatchFrontend',
  18.             'Enlight_Bootstrap_AfterInitResource_shopware_searchdbal.manufacturer_condition_handler_dbal' => 'decorateManufacturerConditionHandler',
  19.             'Enlight_Bootstrap_AfterInitResource_shopware_searchdbal.manufacturer_facet_handler_dbal' => 'decorateManufacturerFacetHandler',
  20.         ];
  21.     }
  22.  
  23.     public function decorateManufacturerConditionHandler()
  24.     {
  25.         Shopware()->Container()->set('shopware_searchdbal.manufacturer_condition_handler_dbal', new ManufacturerConditionHandler());
  26.     }
  27.  
  28.     public function decorateManufacturerFacetHandler()
  29.     {
  30.         $c = Shopware()->Container();
  31.         Shopware()->Container()->set('shopware_searchdbal.manufacturer_facet_handler_dbal', new ManufacturerFacetHandler(
  32.             $c->get('shopware_storefront.manufacturer_service'),
  33.             $c->get('shopware_searchdbal.dbal_query_builder_factory'),
  34.             $c->get('snippets'),
  35.             $c->get('query_alias_mapper')
  36.         ));
  37.     }
  38.  
  39.     public function onPostDispatchFrontend(\Enlight_Event_EventArgs $args)
  40.     {
  41.         /** @var \Enlight_Controller_Action $controller */
  42.         $controller = $args->get('subject');
  43.         $view = $controller->View();
  44.         $container = Shopware()->Container();
  45.         /** @var \Shopware\Bundle\SearchBundle\ProductSearch $searchService */
  46.         $searchService = $container->get('shopware_search.product_search');
  47.  
  48.         /** @var \Shopware\Bundle\StoreFrontBundle\Service\ContextServiceInterface $contextService */
  49.         $contextService = $container->get('shopware_storefront.context_service');
  50.         $context = $contextService->getShopContext();
  51.         $criteria = new Criteria();
  52.         $criteria->removeBaseCondition('category');
  53.         $criteria->resetFacets();
  54.         $category = $context->getShop()->getCategory()->getId();
  55.         $criteria->addBaseCondition(new CategoryCondition([$category]));
  56.  
  57.         $searchResult = $searchService->search($criteria, $context);
  58.  
  59.  
  60.  
  61.         $products = $searchResult->getProducts();
  62.         usort($products, function ( $a,  $b): int {
  63.             return $a->getNumber() <=> $b->getNumber();
  64.         });
  65.  
  66.         $view->assign('allShopProducts', $products);
  67.  
  68.  
  69.  
  70.         $controllerName = $controller->Request()->getParam('controller');
  71.         if ($controllerName === 'detail') {
  72.  
  73.             $manufacturerGateway = $container->get('shopware_storefront.manufacturer_gateway');
  74.             $sArticle = $view->getAssign('sArticle');
  75.             if ($sArticle["attributes"]["core"]) {
  76.                 if ($supplierIds = $sArticle["attributes"]["core"]->get('jvp_autoren')) {
  77.  
  78.                     foreach ($supplierIds as $k => $v) {
  79.                         $supplierIds[] = $v['id'];
  80.                     }
  81.  
  82.                     $suppliers = $manufacturerGateway->getList($supplierIds, $context);
  83.                     $view->assign('authors', $suppliers);
  84.                 }
  85.             }
  86.         }
  87.  
  88.         if ($controllerName === 'cat' || $controllerName === 'listing') {
  89.  
  90.             $manufacturerGateway = $container->get('shopware_storefront.manufacturer_gateway');
  91.             $sArticles = $view->getAssign('sArticles');
  92.             foreach ($sArticles as &$sArticle) {
  93.                 if ($sArticle["attributes"]["core"]) {
  94.                     if ($supplierIds = $sArticle["attributes"]["core"]->get('jvp_autoren')) {
  95.  
  96.                         foreach ($supplierIds as $k => $v) {
  97.                             $supplierIds[] = $v['id'];
  98.                         }
  99.                         $suppliers = $manufacturerGateway->getList($supplierIds, $context);
  100.                         $sArticle["authors"] = $suppliers;
  101.                     }
  102.                 }
  103.             }
  104.             $view->assign('sArticles', $sArticles);
  105.         }
  106.  
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement