2

I want to hide products on frontend that have attribute brand = branda and brandb. Attributes are dropdown and flat tables are enabled. Reason is i am importing alot of products and some of those should be hidden automatically.

My plan is to hide with event <catalog_product_collection_load_before>.

In observer method i have this:

public function filterProductsbyBrand($observer) {
                $AttributeName = 'brand';
                $brand = 'branda';
                $brand2 = 'brandb';
                if (!Mage::app()->getStore()->isAdmin()) {
                    if (isset($observer['collection'])) {
                        $collection = $observer['collection'];
                        $collection->addFieldToFilter($AttributeName, array('neq' => Mage::getResourceModel('catalog/product')->getAttribute($AttributeName)->getSource()->getOptionId($brand)))
                        $collection->addFieldToFilter($AttributeName, array('neq' => Mage::getResourceModel('catalog/product')->getAttribute($AttributeName)->getSource()->getOptionId($brand2)));
                        return $this;
                    }
                }
            }

But this give error and are not working. Error are like this:

exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'e.brand' in 'where clause'' in /var/www/mysite.com/public_html/lib/Zend/Db/Statement/Pdo.php:228

If i run external with this i get correct collection.

$AttributeName = 'brand';
$brand = 'branda';
$brand2 = 'brandb';
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter($AttributeName, array('neq' => Mage::getResourceModel('catalog/product')->getAttribute($AttributeName)->getSource()->getOptionId($brand)))
    ->addFieldToFilter($AttributeName, array('neq' => Mage::getResourceModel('catalog/product')->getAttribute($AttributeName)->getSource()->getOptionId($brand2)));

What am i doing wrong here?

1

Don't do this.

If you are able to filter before, don't filter in frontend. Makes everything slower and harder to maintain.

Fix your import and disable/hide them with visibility.

0

Try this in your observer file

public function filterProductsbyBrand(Varien_Event_Observer $observer)
{
    $collection = $observer->getCollection();
    $attributeName = 'brand';
    $brand1 = 'branda';
    $brand2 = 'brandb';
    if (!Mage::app()->getStore()->isAdmin() && $collection->getSize()) {
        $collection->addAttributeToSelect('*')
            ->addAttributeToSelect('brand')
            ->addFieldToFilter($attributeName, array('neq' => Mage::getResourceModel('catalog/product')->getAttribute($attributeName)->getSource()->getOptionId($brand1)))
            ->addFieldToFilter($attributeName, array('neq' => Mage::getResourceModel('catalog/product')->getAttribute($attributeName)->getSource()->getOptionId($brand2)));
        return $this;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.