32

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');
61

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
17

Getting the Order Details depends on a few components:

  1. The Order (normally order #)
  2. The Contents of the Order (Simple vs Configurable, Invisible etc.)
  3. The Information you're looking to Extract (Pricing vs Other information)

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();
  • getItemsCollection() will actually return the Parent and the Child, confusing for most. Lets focus on the child.
  • Traditionally, the Parent (ie.Configurable) will have the pricing information, where the child (simple) will not. With the Child (simple product) we're able to determine if there is a parent_id (but not the reverse) and we're able to also grab product information from entity_id (not the reverse) as from getAllVisibleItems().
  • 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";
    
            }
        }
    
  • Just a note, you intval the getQtyOrdered, but that can be a decimal value :) – Harry Mustoe-Playfair Feb 9 '16 at 14:07
  • Where I can find all the documentation about model built-in function like addAttributeToSelect. Thank you. – Iftakharul Alam Jun 13 '17 at 7:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.