3

I am overriding the Catalog Search result block but it is only showing title not product listing. Can anyone tell what i missed here?

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\CatalogSearch\Block\Result" type="Test\Test\Block\Result" />
</config>

Result.php

namespace Test\Test\Block;




class Result extends \Magento\CatalogSearch\Block\Result
{

    public function getSearchQueryText()
    {
        return __("Search results for: '%1'", $this->catalogSearchData->getEscapedQueryText());
    }



}
1

4 Answers 4

1

Update your Result.php file as follows

namespace Test\Test\Block;

class Result extends \Magento\CatalogSearch\Block\Result
{
    public function __construct(
        Context $context,
        LayerResolver $layerResolver,
        Data $catalogSearchData,
        QueryFactory $queryFactory,
        array $data = []
    )
    {
        parent::__construct($context, $layerResolver, $catalogSearchData, $queryFactory, $data);
    }

    public function getSearchQueryText()
    {
        return __("Search results for: '%1'", $this->catalogSearchData->getEscapedQueryText());
    }
}
1
1

Please add constructor in your block class as below

namespace Test\Test\Block;

use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
use Magento\Framework\View\Element\Template\Context;
use Magento\CatalogSearch\Helper\Data;
use Magento\Search\Model\QueryFactory;

class Result extends \Magento\CatalogSearch\Block\Result
{
    public function __construct(
        Context $context,
        LayerResolver $layerResolver,
        Data $catalogSearchData,
        QueryFactory $queryFactory,
        array $data = []
    ) {
        parent::__construct($context, $layerResolver, $catalogSearchData, $queryFactory, $data);
    }

    public function getSearchQueryText()
    {
        return __("Search results for: '%1'", $this->catalogSearchData->getEscapedQueryText());
    }
}

============== EDIT ==================

Add catalogsearch_result_index.xml in your layout

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="search.result">
            <arguments>
                <argument name="template" xsi:type="string">Magento_CatalogSearch::result.phtml</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>
6
  • 1
    I doubt that's the solution as the parent constructor will automatically be called if that code is missing Apr 12, 2017 at 7:46
  • Nope not working. Apr 12, 2017 at 8:07
  • Updated my answer, please try it Apr 12, 2017 at 8:43
  • same result i have no problem in overriding i have problem with phtml are not loading. Apr 12, 2017 at 8:48
  • try to set template and see Apr 12, 2017 at 8:57
1

At the end i end up doing with plugin

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\CatalogSearch\Block\Result">
        <plugin name="tesultchange" type="Test\Test\Model\Resultchange" sortOrder="10" /></type>
</config>

Resultchange.php

<?php

namespace Test\Test\Model;

class Resultchange {


    public function aftergetSearchQueryText()
    {
        $test='abc';
        return __("Current Search results for: <span>%1</span>", $test);
    }
}
3
0

I had the same problem while trying to extend \Magento\Catalog\Block\Product\View\GalleryOptions

I had to put the di.xml into etc/frontend/di.xml instead of etc/di.xml

I also had to execute these commands:

php bin/magento setup:upgrade
sudo -u projectuser bin/magento setup:di:compile
php bin/magento cache:flush

app/code/VENDOR/MODULE/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Block\Product\View\GalleryOptions"
                type="<<VENDOR>>\<<MODULE>>\Block\Product\View\GalleryOptions" />
</config>

app/code/VENDOR/MODULE/Block/Product/View/GalleryOptions.php

<?php

namespace <<VENDOR>>\<<MODULE>>\Block\Product\View;

use Magento\Framework\Serialize\Serializer\Json;
use Magento\Catalog\Block\Product\Context;
use Magento\Framework\Stdlib\ArrayUtils;
use Magento\Catalog\Block\Product\View\Gallery;

class GalleryOptions extends \Magento\Catalog\Block\Product\View\GalleryOptions
{

    public function __construct(
        Context $context,
        ArrayUtils $arrayUtils,
        Json $jsonSerializer,
        Gallery $gallery,
        array $data = []
    ) {
        $this->gallery = $gallery;
        $this->jsonSerializer = $jsonSerializer;
        parent::__construct($context, $arrayUtils, $jsonSerializer, $gallery, $data);
    }

    public function getOptionsJson()
    {
        exit("getOptionsJson override called");
       ...
    }

    public function getFSOptionsJson()
    {
        exit("getOptionsJson override called");
        ...
    }
}

Then I executed:

php bin/magento setup:upgrade
sudo -u projectuser bin/magento setup:di:compile
php bin/magento cache:flush

Hint: As you can see there are exits in my function call. If the execution breaks with this exit, then the override was successfull.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.