Requirement: pre-filter products by customer group name to product attribute.
I was looking for the “proper” way of filtering items by multiple custom attribute settings, but couldn’t find out how.
So, the following is a brief explanation of how I’ve got this running in Magento 1.4.1.
There are two ways of editing core code in Magento.
- Wrong: edit files directly in
app/code/core/Mage
- Right: extend modules and keep them in
app/code/local/MyDevCompany
If you’re in a hurry, you might edit core files directly and then later copy the altered code out into correctly extended module files.
For most of my Magento work, this is the route I have taken: get it working in the core then extend it once I’m sure I’m messing with the correct file/method. Just be sure to copy the code out at some point before upgrading the core!
This is how I have extended _getProductCollection()
in app/code/core/Mage/Catalog/Block/Product/List.php
– the method which generates the product list when viewing a category.
The extended file would be saved to app/code/local/MyDevCompany/Catalog/Block/Product/List.php
_productCollection = $layer->getProductCollection(); // fetch the list of schools and their associated IDs $_product = Mage::getModel('catalog/product'); $_attributes = Mage::getResourceModel('eav/entity_attribute_collection') ->setEntityTypeFilter($_product->getResource()->getTypeId()) ->addFieldToFilter('attribute_code', 'stv_school'); $_attribute = $_attributes->getFirstItem()->setEntity($_product->getResource()); $stv_schools = $_attribute->getSource()->getAllOptions(false); $schools = array(); foreach ($stv_schools as $stv_school) { $schools[$stv_school['label']] = $stv_school['value']; } // Filter by school name and non specific items $allowedSchools = array($schools["Non specific"]); $customer_groupID = Mage::getSingleton('customer/session')->isLoggedIn() ? Mage::getSingleton('customer/session')->getCustomerGroupId() : null; if ($customer_groupID !== null) { $groupName = Mage::getSingleton('customer/group')->load($customer_groupID)->getData('customer_group_code'); $allowedSchools[] = $schools[$groupName]; } $this->_productCollection->addAttributeToFilter('stv_school', $allowedSchools); $this->prepareSortableFieldsByCategory($layer->getCurrentCategory()); // ... snipped - be sure to copy correctly from the original source file ... // } }
Reason I have used such a long and ugly method: addAttributeToFilter()
will only play ball with attribute value IDs.
By fetching possible attribute value text and IDs, flipping the data, we can successfully filter by human readable text.
Further reading on how to set up a module for extension, see http://www.magentocommerce.com/wiki/groups/174/changing_and_customizing_magento_code
Hi. Im a noob. So now you know…
I have no ideea what those dudes say ’bout modules in the page you entered.
Could you please provide some… assistance?
The theme im using dosent have multi layered nav… So I was counting on a tech wiz to solve this filtering issue!
regardless of you answer (or silence) i thank you for your code!
Hi Paul,
I’m afraid that I no longer work on this project and my knowledge of the Magento system has left me.
I hope that others are able to offer you more help 🙂
Iain
thank you anyway, friend!