2

Trying to find product collection with custom attribute option 'Show Room' as filter and out of stock that is 'is_in_stock' attribute is equal to 0

 $sAttributeName = 'sale_by';
    $mOptionValue = 'Show Room';
    $_get_show_room_sale_products = Mage::getModel('catalog/product')->getCollection();
    $_get_show_room_sale_products
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('is_in_stock', 0)
    ->addFieldToFilter(
    $sAttributeName,
    array(
        'eq' => Mage::getResourceModel('catalog/product')
                    ->getAttribute($sAttributeName)
                    ->getSource()
                    ->getOptionId($mOptionValue)
    )
    );

But I am getting following error

Fatal error: Call to a member function getBackend() on a non-object in..../app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816

What am i miss here.Please advice

7

For attribute filter -

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToSelect('name','your-attribute');
$collection->addAttributeToFilter('your-attribute', array('eq' => 'attr-value'));

For out of stock filter

$collection->joinField(
                        'is_in_stock',
                        'cataloginventory/stock_item',
                        'is_in_stock',
                        'product_id=entity_id',
                        '{{table}}.stock_id=1',
                        'left'
                )
                ->addAttributeToFilter('is_in_stock', array('eq' => 0));
  • 1
    I have try with same but it return me all product – Mitul Dec 8 '15 at 11:44

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.