Skip to content

Instantly share code, notes, and snippets.

@albingeorge
Created January 18, 2016 12:01
Show Gist options
  • Save albingeorge/3fc3a0b0a4cccdc6ae78 to your computer and use it in GitHub Desktop.
Save albingeorge/3fc3a0b0a4cccdc6ae78 to your computer and use it in GitHub Desktop.
Add an order in magento programatically
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
require_once 'app/Mage.php';
Mage::app();
$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore('default')->getId());
if ('do customer orders') {
// for customer orders:
$customer = Mage::getModel('customer/customer')
->setWebsiteId(1)
->loadByEmail('customer@example.com');
$quote->assignCustomer($customer);
} else {
// for guesr orders only:
$quote->setCustomerEmail('customer@example.com');
}
// $payments = Mage::getSingleton('payment/config')->getActiveMethods();
// echo "<pre>"; print_r($payments); die;
// add product(s)
$product = Mage::getModel('catalog/product')->load(377);
$buyInfo = array(
'qty' => 1,
// custom option id => value id
// or
// configurable attribute id => value id
);
$quote->addProduct($product, 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();
// set shipping and payment methods. assumes freeshipping and check payment
// have been enabled.
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod('flatrate_flatrate');
$quote->getPayment()->importData(array('method' => 'ccsave',
'cc_owner' => 'ffffffffff',
'cc_type' => 'VI',
'cc_number' => "4111111111111111",
'cc_exp_month' => 11,
'cc_exp_year' => 2016,
'cc_cid' => 123));
$quote->collectTotals()->save();
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();
printf("Created order %s\n", $order->getIncrementId());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment