Created
July 15, 2014 14:46
-
-
Save mklooss/9d2e0bdf6ec4340b6209 to your computer and use it in GitHub Desktop.
Add Category Names to CatalogSearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Custom_MyModule_Helper_Search | |
extends Mage_CatalogSearch_Helper_Data | |
{ | |
public function getSearchableDataFromCategory($productId, $storeId) | |
{ | |
$result = array(); | |
$default = Mage::app()->getStore($storeId)->getRootCategoryId(); | |
$adapter = Mage::getSingleton('core/resource')->getConnection('core_read'); | |
$select = $adapter->select() | |
->from('catalog_category_product') | |
->where('product_id = ?', $productId) | |
; | |
$query = $adapter->query($select); | |
$categoryIds = array(); | |
while ($row = $query->fetch()) | |
{ | |
$categoryIds[] = $row['category_id']; | |
} | |
if(count($categoryIds) > 0) | |
{ | |
$categories = Mage::getModel('catalog/category')->getCollection() | |
->addAttributeToSelect(array('path','name')) | |
->addAttributeToFilter('path', array('like' => '%/'.$default.'/%')) | |
->addAttributeToFilter('entity_id', array('in' => $categoryIds)); | |
foreach($categories as $_cat) | |
{ | |
$result[] = $_cat->getName(); | |
$this->_cache[$_cat->getId()] = $_cat->getName(); | |
$path = $_cat->getPathIds(); | |
array_shift($path); // remove id "1" | |
array_shift($path); // remove default store | |
array_shift($path); // remove main category | |
array_pop($path); // remove current category | |
foreach($path as $_id) | |
{ | |
if(!isset($this->_cache[$_id])) | |
{ | |
$name = Mage::getModel('catalog/category')->getCollection() | |
->addAttributeToSelect(array('name')) | |
->addAttributeToFilter('entity_id', $_id) | |
->getFirstItem() | |
->getName(); | |
$this->_cache[$_id] = $name; | |
} | |
$result[] = $this->_cache[$_id]; | |
} | |
} | |
} | |
return array_unique($result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
also for the other Types, like "Configurable" | |
*/ | |
class Custom_MyModule_Model_Catalog_Product_Type_Simple | |
extends Mage_Catalog_Model_Product_Type_Simple | |
{ | |
/** | |
* Retrieve additional searchable data from type instance | |
* Using based on product id and store_id data | |
* | |
* @param Mage_Catalog_Model_Product $product | |
* @return array | |
*/ | |
public function getSearchableData($product = null) | |
{ | |
$product = $this->getProduct($product); | |
$searchData = parent::getSearchableData($product); | |
$categories = Mage::helper('mymodule/search')->getSearchableDataFromCategory($product->getId(), $product->getStoreId()); | |
return array_merge($searchData, $categories); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment