1

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

thanks

$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 programaticaly for not loggedin customer

1
  • Is this the script exactly as you use it because it's not actually valid at the moment? For instance, there is a try catch without the opening try, and loading of the product is wrong - you can't pass 2 ids, only an id and a column to load() Commented Nov 24, 2015 at 9:32

1 Answer 1

0

Add bellow code in your file

// for login customer orders only:
$email = "[email protected]";

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($email);

$quote->setCustomer($customer)
    ->setCheckoutMethod($customer->getMode())
    ->setPasswordHash($customer->encryptPassword($customer->getPassword()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.