Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Adding attribute to filter in Manufacturer.php
 
boban1987
Jr. Member
 
Total Posts:  2
Joined:  2012-09-05
 

I wrote Manufacturer.php file in app/code/local/myown/layoutmods/catalog/block/product/list/ with function. Now when I try to use it loads all products. I checked and that file is called, but it wont filter by manufacturer. Thank you all for advices.

protected function _getProductCollection() {
        
if (is_null($this->_productCollection)) {

            
// Get attribute value id:
            
$manufacturer_name Mage::registry'current_category' )->getName();
            
$productModel Mage::getModel('catalog/product');
            
$attr $productModel->getResource()->getAttribute('manufacturer');
            if (
$attr->usesSource()) {
                $manufacturer_id 
$attr->getSource()->getOptionId($manufacturer_name);
            
}

            
// load and filter product collection
            
$collection Mage::getModel('catalog/product')->getCollection();
            
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
            
$collection->addStoreFilter();
            
$this->_productCollection $collection;
            
$this->_productCollection->addAttributeToSelect'manufacturer' )->addAttributeToFilter'manufacturer', array( 'eq' => $manufacturer_id ) );
        


        
return $this->_productCollection;
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
TreInnova
Guru
 
Avatar
Total Posts:  465
Joined:  2009-02-20
Fano
 
boban1987 - 30 January 2013 11:40 PM

I wrote Manufacturer.php file in app/code/local/myown/layoutmods/catalog/block/product/list/ with function. Now when I try to use it loads all products. I checked and that file is called, but it wont filter by manufacturer. Thank you all for advices.

protected function _getProductCollection() {
        
if (is_null($this->_productCollection)) {

            
// Get attribute value id:
            
$manufacturer_name Mage::registry'current_category' )->getName();
            
$productModel Mage::getModel('catalog/product');
            
$attr $productModel->getResource()->getAttribute('manufacturer');
            if (
$attr->usesSource()) {
                $manufacturer_id 
$attr->getSource()->getOptionId($manufacturer_name);
            
}

            
// load and filter product collection
            
$collection Mage::getModel('catalog/product')->getCollection();
            
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
            
$collection->addStoreFilter();
            
$this->_productCollection $collection;
            
$this->_productCollection->addAttributeToSelect'manufacturer' )->addAttributeToFilter'manufacturer', array( 'eq' => $manufacturer_id ) );
        


        
return $this->_productCollection;
    
}

Dear,
you should put some Mage::log() inside your code to verify the value of $manufacturer_id for example.
Try to print the query with:

$this->_productCollection->printLogQuery(true)
Regards,
FIlippo
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top