Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1003747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:01:30+00:00 2026-05-16T08:01:30+00:00

You have been so helpful in the past that I keep coming back searching

  • 0

You have been so helpful in the past that I keep coming back searching for help and learning.

This time I am trying to get all products that have a quantity greater than 1 and that are in stock (is_in_stock = 1)

    $products = Mage::getModel('catalog/product')->getCollection();    
    //$products->addAttributeToSelect('*');        

    //SELECT `e`.*, `stock`.`qty` FROM `catalog_product_entity` AS `e` LEFT JOIN `cataloginventory_stock_item` AS `stock` ON stock.product_id = e.entity_id       
    $products->getSelect()->joinLeft(      
       array('stock'=>'cataloginventory_stock_item'),     
       'stock.product_id = e.entity_id',      
       array('stock.qty', 'stock.is_in_stock')      
     );

This returns qty and is_in_stock columns attached to the products table. You can test it as follows:

$products->getFirstItem()->getQty();        
$products->getFirstItem()->getIsInStock();        

The issue begins when I try to filter by qty and is_in_stock.

    $products->addFieldToFilter(array(       
       array('Qty','gt'=>'0'),      
       array('Is_in_stock','eq'=>'1'),        
    ));       

This returns – Invalid attribute name never performing filtering. I am guessing it is trying search for e.qty but cannot find it.

So, I tried to filter differently:

$products->getSelect()->where("`qty` > 0");        
$products->getSelect()->where("`is_in_stock` = 1");          

This is not filtering as well even though, if you look at its sql query, (var_dump((string) $products->getSelect())), and run that query in phpMyAdmin, it works.

Alan Storm in his tutorial mentions that ‘The database query will not be made until you attempt to access an item in the Collection’. So, I make the $products->getFirstItem() call but it still not executing the query or filtering in another words.

What am I doing wrong? Any ideas how to filter by attributes that are joined to the table?

Thank you again,

Margots

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:01:31+00:00Added an answer on May 16, 2026 at 8:01 am

    After looking under the hood I learned that _selectAttributes instance field was not assigned in Mage_Eav_Model_Entity_Collection_Abstract class and that is why get exception. A solution usually would be what Jonathan Day suggested above – add addAttributeToFilter() method, however. It will return error since it cannot find such attribute for catalog/product. (qty and in_invetory are in cataloginventory_stock_item). I found two solutions to my problem both required going different direction:

    1. One involved pursuing a way to query the Select statement that I had set for product collection(see above) but somehow it was not resetting the collection with new product. WhenI copied that Sql statment in phpMyAdmin, it worked, so how to query that statement from product collection:

       $stmt = $products->getConnection('core_write')->query($products->getSelect()->__toString());      
      
      while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){    
        echo "<br>Product Id ".$rows['entity_id'];    
      }    
      
    2. Instead of using catalog/product entity table I used the flat table – cataloginventory_stock_item to accomplish the same thing

      $stockItem = new Mage_CatalogInventory_Model_Stock_Item();    
      $stockItems->addQtyFilter('>',0);    
      $stockItems->addFieldToFilter('is_in_stock',array('eq'=>'1'));     
      

    Now there is a collection of all products with qty > 0 and that are in stock.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been having a really hard time debugging this error since the past
Thus far you guys have been wildly helpful with me getting this little ditty
I have been hitting a dead end with this program. I am trying to
I have been trying to mess with the twitter4j library for some time now,
I have been trying to remove this Firebug Console Error from last few hours.
Since I have been mostly using script languages in the past time but now
I have been struggling with this for the past hour and I was wondering
Having trouble creating a static library in XCode4. I have been following this helpful
Have been working on this question for a couple hours and have come close
Have been searching how to convert a dictionary to a string. But the results

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.