Saturday, June 4, 2011

Update Completed Order Details in Magento


Recently I faced a very tough challenge because of stupid mistake made by one of my team member. We were importing orders in Magento through custom script. While importing orders, one supposed to add parent configurable product and a product option. Instead of that he added all the child products and because of that all the orders and its invoices were created with zero price. It was very frustrating situation for me as total number of such orders were around 1,35,0000. So I have to update all those orders. After spending almost a day I came up with a solution and created a PHP script to load that order and update it's details. Following it the code. I will explain all the code segments one by one.

$transaction = Mage::getModel('core/resource_transaction');
$order = Mage::getModel("sales/order")->loadByIncrementId(1000000);
$subTotal = 0.000;

Above code will load an order specified by increment id and get the order created date as we need that date to update some table entries to change data in invoice. Also above array variables are used to hold product prices and skus. Later we need that to change some table entries.

foreach ($order->getItemsCollection() as $orderItem) {    
        $sku = $orderItem->getSku();
        $qty = $orderItem->getQtyOrdered();
        $original_sku = $sku;
        $product = Mage::getModel('catalog/product');
        $newProd = Mage::getModel('catalog/product');
        $newProd = $newProd->load($newProd->getIdBySku($sku));
        $parentIdArray = $newProd->loadParentProductIds()->getData('parent_product_ids');
        $product = $product->load($parent_id);
        $attribute_value = "";
        $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
        $price = $product->getPrice();
        foreach($childProducts as $childProduct)
            if($childProduct->sku == $original_sku){
                $attribute_value = $childProduct->color;                              
                                         'simple_name'=>  $childProduct->getName(),
                                         'product_calculations' =>1,
                $subTotal += $product->getPrice();

Above code will get each order item which is child product in my case and it replaces that order item with parent product and product option which was color in my case.

    $transaction->addCommitCallback(array($order, 'place'));
    $transaction->addCommitCallback(array($order, 'save'));

Above code will save the order. Now if you open order in Magento admin panel. You can see product is changed but if you check order sub total, total paid etc, it will be still zero. Because Magento did not update it automatically. You have to explicitly update it from database. That I will explain you in other post.

I hope this post helps you if you face same situation like me. Do post you suggesstions in comments. Thanks

No comments:

Post a Comment