0

I have overridden my catalog product listproduct in etc/di.xml as:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
                    <!--Overrride for Block catalog rewrite product list toolbar -->
   <preference for="Magento\Catalog\Block\Product\ListProduct" type="Tagalys\Tglssearch\Block\Catalog\Product\ListProduct" />

This is my ListProduct.php (\Tagalys\Tglssearch\Block\Catalog\Product\ListProduct.php)

namespace Tagalys\Tglssearch\Block\Catalog\Product;

use Magento\Catalog\Api\CategoryRepositoryInterface;    

class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
public function __construct(
    /*passing all Constructors parameters to the parent class */    
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
     CategoryRepositoryInterface $categoryRepository,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    /*passing all Constructors parameters to the parent class */    

    \Tagalys\Tglssearch\Helper\Data $tglssearchHelper,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $catalogResourceModelProductCollectionFactory,
    \Magento\Catalog\Model\Config $catalogConfig,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
) {
    $this->tglssearchHelper = $tglssearchHelper;
    $this->catalogResourceModelProductCollectionFactory = $catalogResourceModelProductCollectionFactory;
    $this->catalogConfig = $catalogConfig;
    $this->storeManager = $storeManager;

      parent::__construct(
        $context,
        $postDataHelper,
        $layerResolver,
        $categoryRepository,
        $urlHelper,
        $data
    ); 
    //$this->tglssearchHelper = $tglssearchHelper;
}

protected function _getProductCollection()
{
    print_r('_getProductCollection');die;
    $tagalys = $this->tglssearchHelper->getTagalysSearchData();
     if($tagalys == false) {

        return parent::_getProductCollection();

    } else {

        $searchResult = $tagalys;

        if(empty($searchResult)) {
            return parent::_getProductCollection();
        }

        $collection = $this->_productCollection = $this->catalogResourceModelProductCollectionFactory->create()
             ->addAttributeToSelect($this->catalogConfig->getProductAttributes())
             ->setStore($this->storeManager->getStore())
             ->addFieldToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
             ->addAttributeToFilter( 'entity_id', array( 'in' => $searchResult['results'] ) );  print_r($searchResult);die;

        $orderString = array('CASE e.entity_id');
        foreach($searchResult['results'] as $i => $productId) {
            $orderString[] = 'WHEN '.$productId.' THEN '.$i;
        }
        $orderString[] = 'END';
        $orderString = implode(' ', $orderString);

    $collection->getSelect()->order(new \Zend_Db_Expr($orderString));

        return $this->_productCollection;

    }
}

Why is my _getProductCollection() not getting called ? I get a blank screen when i search for the product.

enter image description here

2
  • if you implements custom search engine better to use existence interfaces. see \Magento\Framework\Search\SearchEngineInterface
    – KAndy
    Jun 22, 2016 at 17:16
  • @Kandy, should i implement the SearchEngineInterface in my custom ListProduct?
    – Sushivam
    Jun 23, 2016 at 6:35

2 Answers 2

0

Well you get a blank screen because your code stops the method.

By calling the following:

print_r('_getProductCollection');die;

You simply stop the process of getting the collection.

That shows that your _getProductCollection is properly getting called.

4
  • yes i had purposefully put print_r in order to check if method is getting called, but not even ''_getProductCollection'' under print_r is getting printed . Even if i remove the print_r() i get a blank
    – Sushivam
    Jun 22, 2016 at 10:52
  • @SachinS well debugging using simple print_r will not work all the time. The fact that you're adding die() and the product list page stops working shows that your method is properly getting called. I suggest you used xDebug to debug your code you will avoid issues like this Jun 22, 2016 at 10:58
  • Raphael, i had removed all die statements in the function before, i was still getting blank page...
    – Sushivam
    Jun 22, 2016 at 11:02
  • .i also get in system.xml LAYOUT_frontend_STORE1_3d41d8cd98f00b204e9800998ecf8427e and handles : Please correct the XML data and try again.
    – Sushivam
    Jun 22, 2016 at 11:10
0

You are missing to complete </config> tag inside your di.xml file,

etc/di.xml file, add below code inside your di.xml file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
                    <!--Overrride for Block catalog rewrite product list toolbar -->
   <preference for="Magento\Catalog\Block\Product\ListProduct" type="Tagalys\Tglssearch\Block\Catalog\Product\ListProduct" />
</config>

Remove var folder and check your problem are solved.

2
  • <config> is already closed Rakesh, i just took a part of the file code and posted
    – Sushivam
    Jun 22, 2016 at 11:14
  • in your di.xml in above code its not closed. Have you check using remove var folder Jun 22, 2016 at 11:18

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.