Arvind Jha
Arvind Jha

Reputation: 67

How to create order programmatically logged in customer?

Currently I make php script to generate order not logged in customer but i want to use this script for logged in customer dynamically for logged in customer my code is

$quote = Mage::getModel('sales/quote')

->setStoreId(Mage::app()->getStore('default')->getId());


// for guest orders only:
$quote->setCustomerEmail('[email protected]');
//}
// add product(s)
$product = Mage::getModel('catalog/product')->load(1307,1305);
$buyInfo = array(
'qty' => 1,
// custom option id => value id
// or
// configurable attribute id => value id
);
$quote->addProduct($product, new Varien_Object($buyInfo));
//$quote->addProduct($product2, new Varien_Object($buyInfo));
$addressData = array(
'firstname' => 'Test',
'lastname' => 'Test',
'street' => 'Sample Street 10',
'city' => 'Somewhere',
'postcode' => '123456',
'telephone' => '123456',
'country_id' => 'US',
'region_id' => 12, // id from directory_country_region table
);
$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('checkmo');
$quote->getPayment()->importData(array('method' => 'checkmo'));
$quote->collectTotals()->save();
echo "quote save";
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
echo "order save";
$order = $service->getOrder();
printf("Created order %s\n", $order->getIncrementId());
}catch(Exception $e){

    print_r($e->getMessage());
}

this script is create order programmatically for not loggedin customer

Upvotes: 2

Views: 103

Answers (1)

Gowtham Raj
Gowtham Raj

Reputation: 26

Arvind,

Follow the same quote object. Use below methods in quote object to create an order for logged in customer.

    $quote->setCustomerId()->setCustomerIsGuest(false)->setCustomerFirstname()->setCustomerLastname()->setCustomerGroupId();

This will create the order for the provided customer information. Hope this will help.

Upvotes: 1

Related Questions