0

I have a custom product attribute soldby which is a dropdown attribute.

I am making a custom product grid. In that grid, I want to display products where soldby=abc.

I have written the following code in Grid.php.

prepareCollection()

$collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('soldby');
$collection->addFieldToFilter(array(
    array('attribute'=>'soldby','eq'=>'abc'),
));

But this doesnot work. Now, no products are shown even though there are products with attribute soldby=abc.

How can I achieve this ?

3
  • If it is an attribute, you can use addAttributeToFilter('soldby', array('eq' => 'abc'))
    – John
    Commented Feb 27, 2017 at 13:07
  • It is not working. P.S: It is a custom product attribute. @John Commented Feb 27, 2017 at 13:09
  • You can try $products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSort("entity_id","DESC") ->addAttributeToSelect('*') ->addAttributeToFilter('soldby', 'abc'); Commented Feb 27, 2017 at 13:13

3 Answers 3

1

You have to pass option id instead of 'abc', and for this you have to get option id first. Try this, it will resolve your issue :

$attributeCode = 'soldby';
$optionText = 'abc';
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product',$attributeCode);
$attr = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
if ($attr->usesSource()) {
    $attributeOptions = $attr ->getSource()->getAllOptions();
    foreach ($attributeOptions as $option) {
    if($option['label']==$optionText){           
        $optionTextId = $curattributeid=$option['value'];
        break;
    }
      }
    }

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter(array(
    array('attribute'=>$attributeCode,'eq'=>$optionTextId),
));
1
  • Thank u.. its working :) Commented Feb 28, 2017 at 6:04
1

Try below code :

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*');
$collection->addAttributeToFilter('soldby', array('eq' => 'abc'));
0

You can use addAttributeToFilter('soldby', 'abc');

protected function _getProductCollection()
    {
       parent::__construct();

        $products = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSort("entity_id","DESC")
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('soldby', 'abc')
            ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds())
            ->setPageSize($this->get_prod_count())
            ->addAttributeToSort($this->get_order(), $this->get_order_dir())
            ->setCurPage($this->get_cur_page());

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
        Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);

        $this->_productCollection = $products;

        return $this->_productCollection;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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