0

I have the following code, where I want it to retrieve a list of simple products from the table sales_flat_order_items

$order = Mage::getModel('sales/order')->load($orderId);
$orderItems = $order->getItemsCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('product_type', array('eq'=>'simple'))
        ->load();

The problem is addAttributeTofilter() is not working, it shows me all items in that table instead of simple products. What did I do wrong?

1
  • try addFieldToFilter instead of addAttributeToFilter
    – Piyush
    May 30, 2017 at 14:09

2 Answers 2

2

try this code , it will return you all simple product from that order

$orderItems = Mage::getModel('sales/order_item')->getCollection()
    ->addFieldToFilter('order_id',$orderId)
    ->addFieldToFilter('product_type', Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
3
  • this code works.... what did I do wrong? so we have to use sales/order_item instead of sales/order? what's the different? May 30, 2017 at 14:23
  • The collection was already loaded in your code
    – Mageinn
    May 30, 2017 at 14:23
  • 3
    $order->getItemsCollection() returned loaded collection and filtering was not possible on the loaded collection.
    – Mageinn
    May 30, 2017 at 14:25
1
$orderItems = Mage::getResourceModel('sales/order_item_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('order_id', $orderId)
        ->addFieldToFilter('product_type', 'simple')
        ->load();
6
  • just tried it. I get the same result as if I used addAttributeToFilter May 30, 2017 at 14:11
  • Can you paste the following output: foreach($orderItems as $item){echo $item->getProductType();}
    – Mageinn
    May 30, 2017 at 14:12
  • There is no "simple" in the output?
    – Mageinn
    May 30, 2017 at 14:15
  • sorry, the output is. "grouped","simple","configurable". this is what my problem is, it simply ignore the addAttributeToFilter or addFieldToFilter. May 30, 2017 at 14:17
  • Try the edited answer
    – Mageinn
    May 30, 2017 at 14:20

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.