0

I've created a button in the back end of my Magento 1.9 website called "Send to Printers" that I need to send JSON data to my printers API. I'm able to get some of the order information using the following:

$orderID = $this->getRequest()->getParam('order_id');
$order = Mage::getModel("sales/order")->load($orderID);
$customer = $order->getShippingAddress()->getData();

Unfortunately though the following code isn't allowing me to get information about the items of that order:

$orderItems = $order->getAllItems();
foreach ($orderItems as $itemId => $item) {
  if ($item->getParentItem()) {
    // This is a simple product assigned to a configurable
    // You can get every single details by loading it
    $product = Mage::getModel('catalog/product');
    $product->load($item->getSku(),'sku');
    // This variable will store all the data
    $data = $product->getData();
} else if ($item->getHasChildren()) {
    // This is a configurable product
}
}

The foreach never reaches any of the if statements. What am I doing wrong?

2 Answers 2

1

Hi check below code may be help you

$OrderNumber = "100000201";//Put your order Number here

$order = Mage::getModel('sales/order')->load($OrderNumber, 'increment_id');

$order->getAllVisibleItems();


$orderItems = $order->getItemsCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('product_type', array('eq'=>'simple'))
    ->load();

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."<br/>";
        //Simple Item Info from Order
        echo "Type: ".$sItem->getProductType()."<br/>";
        echo "Order Id: ".$sItem->getOrderId()."<br/>";
        echo "Product Id: ".$sItem->getProductId()."<br/>";
        echo "Item Id: ".$sItem->getId()."<br/>";
        echo "Item Name: ".$sItem->getName()."<br/>";
        echo "Item Sku: ".$sItem->getSku()."<br/>";
        echo "Item Price: ".$sItem->getPrice()."<br/>";

        $pItemId = $sItem->getParentItemId();
        echo "Parent Item Id: ".$pItemId."<br/>";

        echo "<br/>*****<br/>";
        //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()."<br/>";
        echo "Parent Order Id: ".$item->getOrderId()."<br/>";
        echo "Product Id: ".$item->getProductId()."<br/>";
        echo "Item Id: ".$item->getId()."<br/>";
        echo "Parent Item Price: ".$item->getPrice()."<br/>";
        echo "Qty: ".$qty = intval($item->getQtyOrdered())."<br/>";

        //get Active Product Data
        $nProduct = Mage::getModel('catalog/product')->load($sItem->getProductId());
        $nSku = $nProduct->getSku();
        echo "new Product UPC:".$nUpc = $nProduct->getUpc() . "<br/>";
        echo "new Product Price:".$nPrice = $nProduct->getPrice(). "<br/>";

        }
    }   
1

Magento is insert multiple rows at sales_flat_order_item whenever a configurable product will order.

The sku field are always be simple product sku.But in those multiple rows,one of rows, product_id field value is configurable product id and that row is identified by parent_item_id =null.

So, if you use getAllVisibleItems instead of getAllItems then it will better and product_type field for getting Product type of order item.

No need of use below code for getting product type.

if ($item->getParentItem()) {
   ....
} else if ($item->getHasChildren()) {
   ....
}

Try below code:

$orderItems = $order->getAllItems();
foreach ($orderItems as $itemId => $item) {
    $item->getSku();
        $item->getProductId();
    $item->getProductType(); // use this for getting product type
}
2
  • This does get me the product ID and type, but it gets the ID for the configurable product when actually I need its child ordered simple product ID etc. Commented Mar 26, 2016 at 10:39
  • For getting simple product sku try code Mage::getModel('catalog/product')->getIdBySku($item->getSku());
    – Amit Bera
    Commented Mar 26, 2016 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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