I need to retrieve an order in Magento by its Id. How do I load a specific order by Id?
So far I've gotten the initial query constructed:
Mage::getModel('sales/order');
To load an order by increment id one would do:
Mage::getModel('sales/order')->loadByIncrementId('10000001'); //use a real increment order id here
To load by entity id you would just call load
:
Mage::getModel('sales/order')->load(24999); //use an entity id here
Getting the Order Details depends on a few components:
Load your Order: (db: sales_flat_order)
$OrderNumber = "100000001";//Put your order Number here
$order = Mage::getModel('sales/order')->load($OrderNumber, 'increment_id');
Next, Filter your Collection of Items based on the Order.
What most will do is: (db: sales_flat_order_item)
$order->getAllVisibleItems();
Which will show the visible products. The problem with this is, that it will get the "configurable" item from the collection (which strangely has the child's sku in the record). I find this to be unpredictable in the case of SKU changes as the historical SKU is no longer present. Instead, I find it better to do an alternative approach as follows.
$orderItems = $order->getItemsCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('product_type', array('eq'=>'simple'))
->load();
iterate through the collection of order items
foreach($orderItems as $sItem) {
//Ignore conf for now
//Alt. Mage_Catalog_Model_Product_Type::TYPE_SIMPLE = 'simple';
if($sItem->getProductType() == "simple")
{
echo "\n*********************************\nMage Order #: ".$OrderNumber."\n";
//Simple Item Info from Order
echo "Type: ".$sItem->getProductType()."\n";
echo "Order Id: ".$sItem->getOrderId()."\n";
echo "Product Id: ".$sItem->getProductId()."\n";
echo "Item Id: ".$sItem->getId()."\n";
echo "Item Name: ".$sItem->getName()."\n";
echo "Item Sku: ".$sItem->getSku()."\n";
echo "Item Price: ".$sItem->getPrice()."\n";
$pItemId = $sItem->getParentItemId();
echo "Parent Item Id: ".$pItemId."\n";
echo "\n*****\n";
//Get Parent Item Information
$item = Mage::getModel('sales/order_item')->load("$pItemId"); //use an item_id here
//Testing, want to see whats inside the parent/configurable item?
//print_r($item->toArray());
echo "Parent Type: ".$item->getProductType()."\n";
echo "Parent Order Id: ".$item->getOrderId()."\n";
echo "Product Id: ".$item->getProductId()."\n";
echo "Item Id: ".$item->getId()."\n";
echo "Parent Item Price: ".$item->getPrice()."\n";
echo "Qty: ".$qty = intval($item->getQtyOrdered())."\n";
//get Active Product Data
$nProduct = Mage::getModel('catalog/product')->load($sItem->getProductId());
$nSku = $nProduct->getSku();
echo "new Product UPC:".$nUpc = $nProduct->getUpc() . "\n";
echo "new Product Price:".$nPrice = $nProduct->getPrice(). "\n";
}
}
intval
the getQtyOrdered
, but that can be a decimal value :)
Commented
Feb 9, 2016 at 14:07
addAttributeToSelect
. Thank you.
Commented
Jun 13, 2017 at 7:04