Showing posts with label Programmatically. Show all posts
Showing posts with label Programmatically. Show all posts

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,



Saturday, April 9, 2011

Programmatically add configurable products to Magento Shopping Cart

Hello Guys, 


Here we have another Magento blog. Recently I was working on building a quick shop module for magento. Its like customer can quickly search products based on some attributes. All the products with matching criteria will be displayed with their options. Customer can select product options and enter quantity and those products get added to shopping cart manually. Here all the products will be configurable products. So I created a custom module for it. After relevant products are displayed all the data like product id, quantity, option id and option values will be sent to cart controller page. This is the page where we need to write code to add product in the cart.


There are two way to do this. Following are the two options.


Option 1 : Via query string


Following is the URL format to add product to shopping cart.


$route  = "http://www.your_domain.com/checkout/cart/add?product=272&qty=2&super_attribute[22]=30";


header("Location : ".$route);


Here 272 is the product entity id displayed in magento admin panel. 22 is the attribute associated with this product and 30 is the selected value of that attribute.  



This will add product to the cart and redirect to cart page. Using this approach you can add a a single product to cart. What if we have more than one product. Like in my case customer can select more than one products and options. Following is the another option to add product manually.

Option 2 :


Following is the code to add product to shopping cart.



$params = array(
    'product' => 272,
    'super_attribute' => array(
        22 =>30 ,
    ),
    'qty' => 2,
);


$cart = Mage::getSingleton('checkout/cart'); 
$product = new Mage_Catalog_Model_Product();
$product->load(272); 
$cart->addProduct($product, $params);
$cart->save(); 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


Using above code you can add any number of products to the cart.
Above code will work for only configurable products having only one option. If configurable product has more option then all the option values should be specified.