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?

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

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.