Note that there are some explanatory texts on larger screens.

plurals
  1. POAfter join, cannot filter by attribute qty - getting products from inventory that are in stock
    primarykey
    data
    text
    <p>You have been so helpful in the past that I keep coming back searching for help and learning. </p> <p>This time I am trying to get all products that have a quantity greater than 1 and that are in stock (<code>is_in_stock</code> = 1)</p> <pre><code> $products = Mage::getModel('catalog/product')-&gt;getCollection(); //$products-&gt;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-&gt;getSelect()-&gt;joinLeft( array('stock'=&gt;'cataloginventory_stock_item'), 'stock.product_id = e.entity_id', array('stock.qty', 'stock.is_in_stock') ); </code></pre> <p>This returns <code>qty</code> and <code>is_in_stock</code> columns attached to the products table. You can test it as follows:</p> <pre><code>$products-&gt;getFirstItem()-&gt;getQty(); $products-&gt;getFirstItem()-&gt;getIsInStock(); </code></pre> <p>The issue begins when I try to filter by <code>qty</code> and <code>is_in_stock</code>.</p> <pre><code> $products-&gt;addFieldToFilter(array( array('Qty','gt'=&gt;'0'), array('Is_in_stock','eq'=&gt;'1'), )); </code></pre> <p>This returns - <code>Invalid attribute name</code> never performing filtering. I am guessing it is trying search for <code>e.qty</code> but cannot find it. </p> <p>So, I tried to filter differently:</p> <pre><code>$products-&gt;getSelect()-&gt;where("`qty` &gt; 0"); $products-&gt;getSelect()-&gt;where("`is_in_stock` = 1"); </code></pre> <p>This is not filtering as well even though, if you look at its sql query, (<code>var_dump((string) $products-&gt;getSelect())</code>), and run that query in phpMyAdmin, it works.</p> <p>Alan Storm in <a href="http://alanstorm.com/magento_collections" rel="nofollow noreferrer">his tutorial</a> 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.</p> <p>What am I doing wrong? Any ideas how to filter by attributes that are joined to the table?</p> <p>Thank you again,</p> <p>Margots </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload