Created
January 11, 2013 16:45
-
-
Save SchumacherFM/4512160 to your computer and use it in GitHub Desktop.
Magento (CE1.7) Catalog Category Layer Filter: Filtering for the attribute gender.
Adding a second value to this attribute for the filter query. In this example we add the unisex value to the query when someone searches for a product which gender attribute is lady or gentlemen.
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 | |
/** | |
* XML rewrite: | |
* | |
* <config><global><models> | |
<catalog_resource> | |
<rewrite> | |
<layer_filter_attribute>Namespace_Module_Model_Resource_Layer_Filter_Attribute</layer_filter_attribute> | |
</rewrite> | |
</catalog_resource> | |
* </models></global></config> | |
* | |
* */ | |
class Namespace_Module_Model_Resource_Layer_Filter_Attribute extends Mage_Catalog_Model_Resource_Layer_Filter_Attribute | |
{ | |
/** | |
* Apply attribute filter to product collection | |
* | |
* @param Mage_Catalog_Model_Layer_Filter_Attribute $filter | |
* @param int $value | |
* | |
* @return Mage_Catalog_Model_Resource_Layer_Filter_Attribute | |
*/ | |
public function applyFilterToCollection($filter, $value) | |
{ | |
$collection = $filter->getLayer()->getProductCollection(); | |
$attribute = $filter->getAttributeModel(); | |
$connection = $this->_getReadAdapter(); | |
$tableAlias = $attribute->getAttributeCode() . '_idx'; | |
$conditions = array( | |
"{$tableAlias}.entity_id = e.entity_id", | |
$connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()), | |
$connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()), | |
); | |
$unisexValue = Mage::helper('namespace_module')->getUnisexValue(); | |
if ($attribute->getAttributeCode() === 'gender' && (int)$value !== $unisexValue) { | |
$conditions[] = $connection->quoteInto("{$tableAlias}.value in (?)", array((int)$value, $unisexValue)); | |
} else { | |
$conditions[] = $connection->quoteInto("{$tableAlias}.value = ?", $value); | |
} | |
$collection->getSelect()->join( | |
array($tableAlias => $this->getMainTable()), | |
implode(' AND ', $conditions), | |
array() | |
); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment