Question

I want to place order with customer email id in magento 1.9 For this i need a script means i want to do it programmatically in php Please help asap

Was it helpful?

Solution

Create a php file in your Magento root directory named create_order.php(you can change the file name according to your requirement)

Add the below code to your create_order.php

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('default');

$store = Mage::app()->getStore();
$website = Mage::app()->getWebsite();

//Create sales quote object
$quote = Mage::getModel('sales/quote')->setStoreId($store->getStoreId());

//Customer information
$customerEmail = "example@example.com";
$customerFirstname = "Joe"; 
$customerLastname = "Doe";

$billingAddress = array(
    'customer_address_id' => '',
    'prefix' => '',
    'firstname' => $customerFirstname,
    'middlename' => '',
    'lastname' => $customerLastname,
    'suffix' => '',
    'company' => '', 
    'street' => array(
        '0' => 'Your Street Address 1', // required
        '1' => 'Your Street Address 1' // optional
    ),
    'city' => 'Your City',
    'country_id' => 'US', // country code
    'region' => 'Alaska',
    'region_id' => '2',
    'postcode' => '45263',
    'telephone' => '999-888-0000',
    'fax' => '',
    'save_in_address_book' => 1
);

$shippingAddress = array(
    'customer_address_id' => '',
    'prefix' => '',
    'firstname' => $customerFirstname,
    'middlename' => '',
    'lastname' => $customerLastname,
    'suffix' => '',
    'company' => '', 
    'street' => array(
        '0' => 'Your Street Address 1', // required
        '1' => 'Your Street Address 1' // optional
    ),
    'city' => 'Your City',
    'country_id' => 'US', // country code
    'region' => 'Alaska',
    'region_id' => '2',
    'postcode' => '45263',
    'telephone' => '999-888-0000',
    'fax' => '',
    'save_in_address_book' => 1
);

//Check whether the customer already registered or not
$customer = Mage::getModel('customer/customer')->setWebsiteId($website->getId())->loadByEmail($customerEmail);

if (!$customer->getId()) {

    //Create the new customer account if not registered
    $customer = Mage::getModel('customer/customer'); 
    $customer->setWebsiteId($website->getId())
             ->setStore($store)
             ->setFirstname($customerFirstname)
             ->setLastname($customerLastname)
             ->setEmail($customerEmail);

    try {
        $password = $customer->generatePassword(); 
        $customer->setPassword($password);

        //Set the customer as confirmed
        $customer->setForceConfirmed(true);
        $customer->save();

        $customer->setConfirmation(null);
        $customer->save();

        //Set customer address
        $customerId = $customer->getId(); 
        $customAddress = Mage::getModel('customer/address'); 
        $customAddress->setData($billingAddress)
                      ->setCustomerId($customerId)
                      ->setIsDefaultBilling('1')
                      ->setIsDefaultShipping('1')
                      ->setSaveInAddressBook('1');

        //Save customer address
        $customAddress->save();

        //Send new account email to customer
        $storeId = $customer->getSendemailStoreId();
        $customer->sendNewAccountEmail('registered', '', $storeId);

        //Set password remainder email if the password is auto generated by magento
        $customer->sendPasswordReminderEmail();

    } catch (Exception $e) {
        Mage::logException($e);
    } 
}

//Assign the customer to quote
$quote->assignCustomer($customer);

//Set currency for the quote
$quote->setCurrency(Mage::app()->getStore()->getBaseCurrencyCode());

$productIds = array(500 => 2, 501 => 4); //array('product_id' => 'qty')

//Add products to quote
foreach($productIds as $productId => $qty) {
    $product = Mage::getModel('catalog/product')->load($productId);
    $quote->addProduct($product, $qty);
}

//Add billing address to quote
$billingAddressData = $quote->getBillingAddress()->addData($billingAddress);

//Add shipping address to quote
$shippingAddressData = $quote->getShippingAddress()->addData($shippingAddress);

//Collect shipping rates on quote
$shippingAddressData->setCollectShippingRates(true)->collectShippingRates();

//Set shipping method and payment method on the quote
$shippingAddressData->setShippingMethod('flatrate_flatrate')->setPaymentMethod('checkmo'); //Shipping is flatrate for this example

//Set payment method for the quote
$quote->getPayment()->importData(array('method' => 'checkmo')); //Payment is check and money order for this example

try {
    //Collect totals & save quote
    $quote->collectTotals()->save();

    //Create order from quote
    $service = Mage::getModel('sales/service_quote', $quote);
    $service->submitAll();
    $increment_id = $service->getOrder()->getRealOrderId();

    echo 'Order Id: ' .$increment_id. ' has been successfully created.';

} catch (Exception $e) {
    Mage::logException($e);
}
?>

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top