如何在Magento 2中覆盖目录搜索结果?

时间:2019-03-17 10:20:38

标签: php magento magento2

我正在使用自定义插件,我需要覆盖目录产品搜索结果。

例如,总是在结果中添加\删除某些产品。

类似于Wordpress的the_posts过滤器。


如果这不可能,我可以覆盖目录搜索结果html吗?

1 个答案:

答案 0 :(得分:0)

是的,您可以通过在自定义模块中进行以下更改来覆盖目录搜索结果 在您的控制器中

<?php
namespace Vendor\Module\Controller\Search;
class Result extends \Magento\CatalogSearch\Controller\Result\Index
{
public function execute($coreRoute = null)
{
return parent::execute($coreRoute);
}
}

在您的街区     

<?php

$query = $this->getRequest()->getParam('q');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productFactory = $objectManager->create('Magento\Catalog\Model\ProductFactory');
$collection = $productFactory->create()->getCollection();

$query = $this->getRequest()->getParam('q');

?>

<div>
    <form class="form minisearch" id="searchProducts"  method="get">
         <div class="col-xs-12 col-md-6 col-sm-3">
            <input type="text" name="q" id="searchbox" value="" class="searchbox" placeholder="Type here...." />
          </div>
          <div class="col-xs-6 col-md-2 col-sm-3"> 
            <button type="submit" class="btn new-searchbtn action search menu-search" id="submitBtn" title="Search">
              <span class="search-text">search</span>
              <span class="search-icon"></span>
            </button>
          </div>
    </form>
</div>
<?php if ($collection->count()): ?>
<?= $block->getChildHtml('tagged_product_list_rss_link') ?>
<div class="search results container">
    <?php if ($messages = $block->getNoteMessages()):?>
    <div class="message notice">
        <div>
            <?php foreach ($messages as $message):?>
                <?= /* @escapeNotVerified */ $message ?><br />
            <?php endforeach;?>
        </div>
    </div>
    <?php endif; ?>
    <?= $block->getProductListHtml() ?>
</div>
<?php// else: ?>
<?php endif; ?>

最后,您的vendor / module / view / frontent / layout / catalogsearch_result_index.xml将如下所示。

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

         <referenceBlock name="search.result">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Module::result.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>