How to Get Orders for Specific Product Attribute in Magento 1.9

magento-1.9product-attribute

I want to apply filter as follow, I want to get All orders having product of specific attribute, my product has a custom attribute ii_id,my sql query may be like this "get all orders having product where ii_id==123", here more than two tables (sales_flat_order,sales_flat_order_item,and tables for product)are involved, I am bit confused, how should I do this?

Best Answer

$productId = 123;
$orders = array();
$collection = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToFilter('product_id', array('eq' => $productId))
    ->load();
foreach($collection as $orderItem) {
  echo $orderItem->getOrder();
}
Related Topic