_storeManager = $storeManager; $this->_queryFactory = $queryFactory; $this->layerResolver = $layerResolver; $this->_resultJsonFactory = $resultJsonFactory; $this->_resultPageFactory = $resultPageFactory; $this->_catalogSearchHelper = $catalogSearchHelper; $this->_wpHelper = $wpHelper; } /** * @param \Magento\CatalogSearch\Controller\Result\Index $subject * @param \Closure $method * @return $this|array|mixed * @throws \Magento\Framework\Exception\LocalizedException */ public function aroundExecute( \Magento\CatalogSearch\Controller\Result\Index $subject, \Closure $method ) { if ($subject->getRequest()->getParam('ajax') == 1 && $this->_wpHelper->isEnabled()) { $this->_wpHelper->updateSliderBodyClass(); $requestUri = $subject->getRequest()->getRequestUri(); $requestUri = preg_replace('/(\?|&)ajax=1/', '', $requestUri); $subject->getRequest()->setRequestUri($requestUri); try { $this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH); } catch (\Exception $ex) { } /* @var $query \Magento\Search\Model\Query */ $query = $this->_queryFactory->get(); $query->setStoreId($this->_storeManager->getStore()->getId()); $resultJson = $this->_resultJsonFactory->create(); if ($query->getQueryText() != '') { if ($this->_catalogSearchHelper->isMinQueryLength()) { $query->setId(0)->setIsActive(1)->setIsProcessed(1); } else { $query->saveIncrementalPopularity(); if ($query->getRedirect()) { $data = [ 'success' => true, 'redirect_url' => $query->getRedirect() ]; return $resultJson->setData($data); } } $this->_catalogSearchHelper->checkNotes(); $pageFactory = $this->_resultPageFactory->create(); $resultsBlockHtml = $pageFactory->getLayout()->getBlock('search.result')->toHtml(); $leftNavBlockHtml = $pageFactory->getLayout()->getBlock('catalogsearch.leftnav')->toHtml(); $dataLayerContent = ''; $dataLayerContentGa4 = ''; $dataLayerBlock = $pageFactory->getLayout()->getBlock('head.additional'); if ($dataLayerBlock) { $dLBlockHtml = $dataLayerBlock->toHtml(); preg_match('/var dlObjects ?= ?(.*?)];/', $dLBlockHtml, $matches); if (count($matches) == 2) { $dataLayerContent = $matches[1] . ']'; } preg_match('/var dl4Objects ?= ?(.*?)];/', $dLBlockHtml, $matchesGa4); if (count($matchesGa4) == 2) { $dataLayerContentGa4 = $matchesGa4[1] . ']'; } } $ga4ServerSideViewItemListContent = ''; $ga4ServerSideViewItemListBlock = $pageFactory->getLayout()->getBlock('weltpixel-ga4-server-view-item-list'); if ($ga4ServerSideViewItemListBlock) { $ga4ServerSideViewItemListBlockHtml = $ga4ServerSideViewItemListBlock->toHtml(); preg_match('/_resultJsonFactory->create()->setData( [ 'success' => true, 'html' => [ 'products_list' => $resultsBlockHtml, 'filters' => $leftNavBlockHtml, 'dataLayer' => $dataLayerContent, 'dataLayerGA4' => $dataLayerContentGa4, 'ga4ServerSideItemListHash' => $ga4ServerSideViewItemListContent ] ] ); } else { $data = [ 'success' => true, 'redirect_url' => $this->_redirect->getRedirectUrl() ]; return $resultJson->setData($data); } } else { return $method(); } } }