Saturday, May 7, 2011

Programmatically create order in Magento

Hello,

This is the toughest work I ever did in Magento. Since last few days I am working on Magento and Channel Advisor(CA) Integration.  The requirement was to fetch order from CA and process them in Magento. We were using Channel Advisor PHP API to fetch orders from CA.

CA order service API needs order criteria to fetch orders. Following is the order criteria we were passing.


$OrderCriteria = array(
'OrderCreationFilterBeginTimeGMT'=> $date1,
'OrderCreationFilterEndTimeGMT'=> $date2,
'StatusUpdateFilterBeginTimeGMT' => $date1,
'StatusUpdateFilterEndTimeGMT' => $date2,
'JoinDateFiltersWithOr' => true,
'DetailLevel' => 'Complete',
'ExportState' => 'NotExported',
'OrderStateFilter' => 'Active',
'PaymentStatusFilter' => 'Cleared',
'CheckoutStatusFilter'  => 'Completed',
'ShippingStatusFilter'  =>'Unshipped',
'PageNumberFilter'=>1,
'PageSize'=>20
);



$arrData = array(
        'accountID'=>$accountID,
        'orderCriteria'=>$OrderCriteria
);
$result=$client->GetOrderList($arrData);               
$results=$result->GetOrderListResult->ResultData;
$orders=$results->OrderResponseItem ;

It will return first 20 orders that match above criteria.

Now here is the tough job. We have to fetch all the order data and have to create order in Magento. Following is the code for it.


foreach($orders as $order)
{

$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()
->getStore('default')->getId());
$customer = Mage::getModel('customer/customer')
->setWebsiteId(1)
->loadByEmail($order->BuyerEmailAddress);                  
                   
if(empty($customer)){
$quote->assignCustomer($customer);
}
else{
$quote->setCustomerEmail($order->BuyerEmailAddress);
}

$orderitems = $order->ShoppingCart->LineItemSKUList->OrderLineItemItem;


foreach($orderitems as $orderItem){
        $product = $newProd
                               ->load($newProd->getIdBySku($orderItem->SKU));

        $buyInfo = array(
         'qty' => $orderItem->Quantity
         );

         $quote->addProduct($product, new Varien_Object($buyInfo));                    

}


$region=$order->ShippingInfo->Region;
$regionid = GetRegionId($region);


$firstName = ($order->BillingInfo->FirstName != "")
? ($order->BillingInfo->FirstName)
:($order->ShippingInfo->FirstName);
if(empty($firstName)){
$firstName = "FirstName";  
}

$firstName = ($order->BillingInfo->FirstName != "") 
? ($order->BillingInfo->FirstName) 
:($order->ShippingInfo->FirstName);
if(empty($firstName)){
$firstName = "FirstName";    
}

$lastName = ($order->BillingInfo->LastName != "") 
? ($order->BillingInfo->LastName) 
: ($order->ShippingInfo->LastName);
if(empty($lastName)){
$lastName = "LastName";    
}
                    
$street = (($order->BillingInfo->AddressLine1 != "")
? ($order->BillingInfo->AddressLine1) 
: ($order->ShippingInfo->AddressLine1)).
(($order->BillingInfo->AddressLine2 != "") 
? ($order->BillingInfo->AddressLine2) 
: ($order->ShippingInfo->AddressLine2));

if(empty($street)){
$street = "Street";    
}
                    
$city = ($order->BillingInfo->City != "") 
? ($order->BillingInfo->City) 
: ($order->ShippingInfo->City);
if(empty($city)){
$city = "City";    
}
                    
$postCode = ($order->BillingInfo->PostalCode != "") 
? ($order->BillingInfo->PostalCode) 
: ($order->ShippingInfo->PostalCode);
if(empty($postCode)){
$postCode = "00000";    
}
                    
$telephone = ($order->BillingInfo->PhoneNumberDay != "") 
? ($order->BillingInfo->PhoneNumberDay) 
: ($order->ShippingInfo->PhoneNumberDay);

if(empty($telephone)){
$telephone = "00000";    
}
                    
$addressData = array(
'firstname' => $firstName,
'lastname' => $lastName,
'street' => $street,
'city' => $city,
'postcode' => $postCode,
'telephone' => $telephone,
'country_id' => $order->ShippingInfo->CountryCode,
'region_id' => $regionid
);

$billingAddress = $quote
                             ->getBillingAddress()
                             ->addData($addressData);

$firstName = ($order->ShippingInfo->FirstName != "") 
? ($order->ShippingInfo->FirstName) 
: ($order->BillingInfo->FirstName);
if(empty($firstName)){
$firstName = "FirstName";    
}
                    
$lastName = ($order->ShippingInfo->LastName != "") 
? ($order->ShippingInfo->LastName) 
: ($order->BillingInfo->LastName);
if(empty($lastName)){
$lastName = "LastName";    
}
                    
$street = (($order->ShippingInfo->AddressLine1 != "") 
? ($order->ShippingInfo->AddressLine1) 
: ($order->BillingInfo->AddressLine1)).
(($order->ShippingInfo->AddressLine2 != "") 
? ($order->ShippingInfo->AddressLine2) 
: ($order->BillingInfo->AddressLine2));                    
if(empty($street)){
$street = "Street";    
}
                    
$city = ($order->ShippingInfo->City != "") 
? ($order->ShippingInfo->City) 
: ($order->BillingInfo->City);
if(empty($city)){
$city = "City";    
}
                    
$postCode = ($order->ShippingInfo->PostalCode != "") 
? ($order->ShippingInfo->PostalCode) 
: ($order->BillingInfo->PostalCode);
if(empty($postCode)){
$postCode = "00000";    
}
                    
$telephone = ($order->ShippingInfo->PhoneNumberDay != "") 
? ($order->ShippingInfo->PhoneNumberDay) 
: ($order->BillingInfo->PhoneNumberDay);
if(empty($telephone)){
$telephone = "00000";    
}
                    
$addressData = array(
'firstname' => $firstName,
'lastname' => $lastName,
'street' => $street,
'city' => $city,
'postcode' => $postCode,
'telephone' => $telephone,
'country_id' => $order->ShippingInfo->CountryCode,
'region_id' => $regionid
);

$shippingAddress = $quote->getShippingAddress()->addData($addressData);
                    
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
                        ->setShippingMethod('perproductshipping_ground')
                        ->setPaymentMethod('purchaseorder');
                        
$quote->getPayment()->importData(array(
'method' => 'purchaseorder',
'po_number' => $order->ClientOrderIdentifier));
         
$quote->collectTotals()->save();
                    
Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "0"); 
                    
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();




}

That's it and it will create new purchase order in magento. Limitations of above code are as follow.

1) It will only create purchase order
2) It will only be useful for simple products.

I hope this post helps you.

Thanks,



Wednesday, May 4, 2011

Cloud Computing part-2 Windows Azure

Hello,

This is going to be theoretical blog. In my last blog on cloud computing I explained what exactly is cloud computing.  In this blog I will explain why one should go for cloud computing. Mostly it will be related to Microsoft Windows Azure .Recently I attended a Windows Azure Training Camp where speaker gave a good example of it.

First of all let's see how windows azure works. Cloud computing is all about virtulization. When you deploy your application on azure cloud your application is actually running on virtual machine. Here you can configure number of instance of application. For example if you set number of instances to three. Your application is running on three different virtual machine. Application load is automatically balanced between these virtual machines. You don't have to worry about anything. So this is how windows azure works. Now let's see an example on how windows azure can help you.

Consider you have created an application which consumes more server resources than any other application. Now initially you have 100 users of application. So you buy a server and deployed your application. You are happy, your users are happy too. Now number of users increase to say 1000. Now your single server can not fulfill requirements. Now your users are not happy. So you buy another server for load balancing. After some time again number of users increase and you have to buy another server. This is how you buy almost 20 servers. Each month you have to pay for those servers. Now suddenly number of users decrease and now you can run application only on 5 servers and this situation continues. So what about other 15 servers? Those servers are not utilized and are ideal and still you have to pay for each sever per month.

Now let's see power of Windows Azure. Consider same scenario as above with Windows Azure. Initially you application is running on tow instances. When number of users increase you simply have to increase number of instances. Now your application is running on 20 instances and you are paying for  20 instances per month. Now number of user decreases and you have to reset number of instances to 5. That's it now you don't need to pay for other 15 instances because you are not using it. You only have to pay for 5 instances that you are using.

So it gives you flexibility on number of instances of your application. Anytime you can change it and you have to only pay for the resources which are in use. So that's the power of cloud computing and windows azure.

Hope this posts help you. Being a programmer usually I don't like to write theoretical blogs but still it's necessary to write. Now next posts will be on examples windows azure.