Saturday, June 4, 2011

Update Completed Order Details in Magento

Hello,

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;
$date=$order->created_at;
$order_id=$order->getId();
 
$arr_sku=array();
$arr_price=array();
$arr_par_sku=array();
$arr_qty=array();


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();
        $arr_qty[]=$qty;
        $original_sku = $sku;
        $arr_sku[]=$original_sku;
        $product = Mage::getModel('catalog/product');
        $newProd = Mage::getModel('catalog/product');
        $newProd = $newProd->load($newProd->getIdBySku($sku));
                           
        $parentIdArray = $newProd->loadParentProductIds()->getData('parent_product_ids');
       
        $parent_id=$parentIdArray[0];                                      
        $product = $product->load($parent_id);
     
        $attribute_value = "";
        $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
        $par_sku=$product->getSku();
        $arr_par_sku[]=$par_sku;
        $price = $product->getPrice();
        $arr_price[]=$price;
        foreach($childProducts as $childProduct)
        {
            if($childProduct->sku == $original_sku){
                $attribute_value = $childProduct->color;                              
                $attribute_text=$childProduct->getAttributeText('color');
             
               $attribute_data=array('info_buyRequest'=>array(
                                                'product'=>$childProduct->getId(),
                                                'super_attribute'=>array(
                                                                          '80'=>$attribute_value,
                                                                            )
                                                ),
                                        'attributes_info'=>array(
                                                               '0'=>array(
                                                                        'label'=>'Color',
                                                                        'value'=>$attribute_text
                                                                            )                                                                    
                                                                ),
                                         'simple_name'=>  $childProduct->getName(),
                                         'simple_sku'=>$childProduct->getSku(),
                                         'product_calculations' =>1,
                                         'shipment_type'=>0
                        );  
                     
                $orderItem->setProductOptions($attribute_data);
                $orderItem->setProductId($product->getId());
                $orderItem->setProductType('configurable');
                $orderItem->setQtyOrdered($qty);
                $orderItem->setName($product->getName());
                $orderItem->setSku($product->getSku());
                $orderItem->setPrice($product->getPrice());
                $orderItem->setBasePrice($product->getPrice());
                $orderItem->setOriginalPrice($product->getPrice());
                $orderItem->setRowTotal($product->getPrice());
                $orderItem->setBaseRowTotal($product->getPrice());              
                $subTotal += $product->getPrice();
                break;
            }
        } 
    }


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.


$price2=$subTotal+6.95;
    $order->setSubtotal($subTotal)
            ->setBaseSubtotal($subTotal)
            ->setGrandTotal($subTotal)
            ->setBaseGrandTotal($subTotal);
    $transaction->addObject($order);
    $transaction->addCommitCallback(array($order, 'place'));
    $transaction->addCommitCallback(array($order, 'save'));
    $transaction->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