Get a list of orders in magento extension that hav

2019-08-06 04:53发布

问题:

How would I get a list of all orders in Magento that have a certain product on the order?

I have built an extension and need to know all orders which contain a certain product.

回答1:

This is not a duplicate question per se, so here a solution that might work for you:

$productId = {PRODUCT_ID};
$orders = array();
$collection = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToFilter('product_id', array('eq' => $productId))
    ->load();
foreach($collection as $orderItem) {
    $orders[$orderItem->getOrder()->getIncrementId()] = $orderItem->getOrder();
}

You'll end up with an array of orders which contain an orderitem for the given {PRODUCT_ID}.



标签: php magento