how to magento Custom search programmatically
In this post we will show you how to magento Custom search programmatically, hear for how to magento Custom search programmatically we will give you demo and example for implement.
this is a code a controller intended to expose Magento’s catalog search functionality to API calls. Using filters with the default REST api just doesn’t return the same quality results as the search feature available on a Magento site.
Magento search model is more intricate as they assembled a component to spare questions and comes about for storing and measurements. In this way, you have to set up the question protest, at that point set up the outcome and after that you can join the item accumulation with the query output table. Here is the manner by which to do it in code.
Method 1 :: magento Custom search programmatically
$searchText = 'add you text'; $custom_query = Mage::getModel('catalogsearch/query')->setQueryText($your_search_text)->prepare(); $fulltext_resource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult( Mage::getModel('catalogsearch/fulltext'), $your_search_text, $custom_query ); $catalog_collection = Mage::getResourceModel('catalog/product_collection'); $catalog_collection->getSelect()->joinInner( array('search_result' => $catalog_collection->getTable('catalogsearch/result')), $catalog_collection->getConnection()->quoteInto( 'search_result.product_id=e.entity_id AND search_result.query_id=?', $custom_query->getId() ), array('relevance' => 'relevance') );
$catalog_collection is a filtered down catalog product collection of which we can then perform any additional operations depending on what we need :
$catalog_collection->setStore(Mage::app()->getStore()); $catalog_collection->addMinimalPrice(); $catalog_collection->addFinalPrice(); $catalog_collection->addTaxPercents(); $catalog_collection->addStoreFilter(); $catalog_collection->addUrlRewrite(); Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($catalog_collection); Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($catalog_collection);
Method 2 :: magento Custom search programmatically
$searchText = 'add you text'; $setsearchQuery = $this->getRequest()->getPost($searchText); $set_collection = Mage::getModel('catalog/product') ->getCollection(); $set_collection->addAttributeToFilter('name', array( //array('like' => '%'.$setsearchQuery.'%'), //spaces on each side //array('like' => '%'.$setsearchQuery), // space before and ends with $needle array('like' => $setsearchQuery.'%') // starts with needle and space after )); echo "<pre>"; print_r($set_collection->getData()); echo "</pre>";
Method 3 :: magento Custom search programmatically
$searchText = 'add you text'; $storeId = '1'; // add your store id $catalog_collection = Mage::getResourceModel("catalog/product_collection")->addAttributeToSelect("*"); $custom_query = Mage::helper("catalogSearch")->getQuery(); $custom_query->setStoreId($storeId); $custom_query->setQueryText($searchText); $catalog_collection = $custom_query->getSearchCollection(); $catalog_collection->addSearchFilter($searchText); $catalog_collection->addAttributeToSelect("*"); $catalog_collection->addAttributeToFilter("status", 1); foreach($catalog_collection as $product_val) { // do something you want }
Hope this code and post will helped you for implement magento Custom search programmatically. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org