Magento search section is a bit more complex as they built a mechanism to save queries and results for caching and statistics. So, you need to prepare the query object, then prepare the result and then you can join the product collection with the search result table.
<?php /* created by :Rinky Kandwal created date : Description : Search Product List by keyword ?q= */ ?> <?php require_once '../app/Mage.php'; Mage::app(); //header("Content-Type: application/json"); $searchText = $_REQUEST['q']; $query = Mage::getModel('catalogsearch/query')->setQueryText($searchText)->prepare(); $fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult( Mage::getModel('catalogsearch/fulltext'),$searchText,$query); $collection = Mage::getResourceModel('catalog/product_collection'); $collection->getSelect()->joinInner( array('search_result' => $collection->getTable('catalogsearch/result')), $collection->getConnection()->quoteInto( 'search_result.product_id=e.entity_id AND search_result.query_id=?', $query->getId() ), array('relevance' => 'relevance') ); $collection->setStore(Mage::app()->getStore()); $collection->addMinimalPrice(); $collection->addFinalPrice(); $collection->addTaxPercents(); $collection->addStoreFilter(); $collection->addUrlRewrite(); $searchresult1=Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); $searchresult2=Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection); $obj->collection = $collection; // get original images of product section $productMediaConfig = Mage::getModel('catalog/product_media_config'); // get current currency $currency_code = Mage::app()->getStore()->getCurrentCurrencyCode(); $countrecord = count($collection); $j=0; if($countrecord > 0) { foreach($collection as $pc) { //get product id if($pc->getId()!='') { $prodid = $pc->getId(); } else { $prodid=''; } // get product category model $product2 = Mage::getModel('catalog/product')->load($pc->getId()); // get productname if($product2->getName()!='') { $productname = $product2->getName(); } else { $productname=''; } if($product2->getThumbnail()!='') { $thumbnailUrl = $productMediaConfig->getMediaUrl($product2->getThumbnail()); } else { $thumbnailUrl=''; } //get Product's Regular Price if($product2->getPrice()!='') { $regularprice= $product2->getPrice(); } else { $regularprice=''; } //get Product's Special price if($product2->getSpecialPrice()!='') { $specialprice= $product2->getSpecialPrice(); } else { $specialprice=''; } $productlist[$j]=array("productId"=>$prodid,"productName"=>$productname,"thumbnailUrl"=>$thumbnailUrl,"currencyCode"=>$currency_code,"regularPrice"=>$regularprice,"specialPrice"=>$specialprice); $j++; } $data['responseCode']='1'; $data['msg']='successfull'; $data['keyword']=$searchText; $data['currency']=$currency_code; $data['searchProdResult']=$productlist; } else { $data['responseCode']='0'; $data['msg']='No Record Found'; } $data2=json_encode($data); echo $data2; ?>
1 comments:
nice post.
Sap ABAP On Hana training
SAP Grc training
SAP Secrity training
oracle sql plsql training
EmoticonEmoticon