Search code examples
magentosearchcollectionscatalog

How to add custom product on catalog search collection?


Currently i am trying to add custom product on CatalogSearch product collection . Currently i am working on Mage_CatalogSearch_Model_Layer. I know that its core part but after working my code properly i manage with override CatalogSearch_Model.

Currently i am working on this method

 public function prepareProductCollection($collection)
    {
        $collection
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
            ->setStore(Mage::app()->getStore())
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addStoreFilter()
            ->addUrlRewrite();

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);

// my custom code start

$collection2 = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id', array(1,2));

$collectiondata=$collection2->getData();

foreach($collectiondata as $customdata)
{
$collection->addItem($customdata);
}

        return $this;
    }

this code add my custom product to collection but issue start when when in search result no product found.Means if in search product found then it add my custom product otherwise not.

I want to know where i am making mistake ? or any step missing. Any help will be appreciated

Thank you


Solution

  • here the issue:

    $collection2 = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id', array(1,2));
    foreach($collection2 as $customdata)
    {
    $collection->addItem($customdata);
    }
    

    $collection2->getData(); is give not proper object for $customdata.

    $customdata is give proper object of Mage_Catalog_Model_Product