开发者

After join, cannot filter by attribute qty - getting products from inventory that are in stock

开发者 https://www.devze.com 2023-01-10 21:50 出处:网络
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_

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();        
$pro开发者_开发问答ducts->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


I would suggest that you try using $products->addAttributeToFilter... instead of $products->addFieldToFilter... - the addField method only works when the field is on the main table that you are querying (in this case catalog_product_entity). Because the inventory fields are in a joined table, you need to use addAttribute.

Hope this helps, JD


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

精彩评论

暂无评论...
验证码 换一张
取 消