5 min read

Creating Magento order programmatically and charging saved credit card.

Creating Magento order programmatically and charging saved credit card.

Last modified

Creating order is the basic functionality of Magento but its the trickiest one at the same time for developers. Yes, its true because its not straight forward like instantiating model, assigning some values and saving it. Sometimes you may want to create auto orders using some cron script.

Below is a working script that can create orders provided payment method “Bank Transfer” and “Flat Rate” shipping method. You can change it as per your shipping and payment method.


$store_id = '1'; // replace if you are running script from frontend Mage::app()->getStore()->getId()
$customer_id = 'INSERT_CUSTOMER_ID_HERE'; // Mage::getSingleton('customer/session')->getId(); for current logged in user from frontend

/*------------------------------------------------------------------------
Payment and Shipping Method
------------------------------------------------------------------------*/
$payment_method = 'banktransfer';
$shipping_method = 'flatrate_flatrate';


 
/*------------------------------------------------------------------------
Load Customer
------------------------------------------------------------------------*/
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId($store_id);
$customer->loadByEmail($customer_id);

 
/*------------------------------------------------------------------------
Create a Quote
------------------------------------------------------------------------*/
$quote = Mage::getModel('sales/quote');
 
$quote->setStoreId($store_id);
 
$quote->assignCustomer($customer);
 
$quote->setSendCconfirmation(1);
 
/*------------------------------------------------------------------------
Add products to Quote
------------------------------------------------------------------------*/

$product = Mage::getModel('catalog/product')->load(554);
 
$quote->addProduct($product,
    new Varien_Object(
        array(
            'qty' => 1
        )
    )
);

/*------------------------------------------------------------------------
Assign Address, Shipping and Payment methods
------------------------------------------------------------------------*/
$address_data = array(
    'firstname' => 'Tahir',
    'lastname' => 'Yasin',
    'street' => array(
        '0' => 'Sample Street'
    ),
    'city' => 'Sample City',
    'postcode' => '01234',
    'telephone' => '0123456789',
    'country_id' => 'MY',
    'region_id' => NULL,
);
$billingAddress = $quote->getBillingAddress()->addData($address_data);
$shippingAddress = $quote->getShippingAddress()->addData($address_data);
 
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
    ->setShippingMethod($shipping_method)
    ->setPaymentMethod($payment_method);
 
$quote->getPayment()->importData(array('method' => $payment_method));
 
$quote->collectTotals()->save();
/*----------------------------------------------------------------------*/
 
/*------------------------------------------------------------------------
SECTION : CHECKOUT
------------------------------------------------------------------------*/
$convertQuote = Mage::getModel('sales/convert_quote');
 
if ($quote->getIsVirtual()) {
    $order = $convertQuote->addressToOrder($quote->getBillingAddress());
} else {
    $order = $convertQuote->addressToOrder($quote->getShippingAddress());
}
 
// assign payment method
$quotePayment = $quote->getPayment();
 
$quotePayment->setMethod($quote->getPayment()->getMethod());
 
$quote->setPayment($quotePayment);
 
$orderPayment = $convertQuote->paymentToOrderPayment($quotePayment);
 
$order->setBillingAddress($convertQuote->addressToOrderAddress($quote->getBillingAddress()));
$order->setPayment($convertQuote->paymentToOrderPayment($quote->getPayment()));
 
if (!$quote->getIsVirtual()) {
    $order->setShippingAddress($convertQuote->addressToOrderAddress($quote->getShippingAddress()));
}
 
// set payment options
$order->setPayment($convertQuote->paymentToOrderPayment($quote->getPayment()));
 
// order products
$items = $quote->getAllItems();
 
foreach ($items as $item) {
    //@var $item Mage_Sales_Model_Quote_Item
    $orderItem = $convertQuote->itemToOrderItem($item);
 
    if ($item->getParentItem()) {
        $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
    }
 
    $order->addItem($orderItem);
}
 
$order->setCanShipPartiallyItem(false);
$order->sendNewOrderEmail();
$order->place();
$order->save();
$quote->setIsActive(0);
$quote->save();

If you are using Authorize.net CIM module and want to charge saved credit cards automatically using above script. All you need is to change shipping method name to ‘authnetcim’ and pass card_id parameter to importData function as shown below.


$savedCards = Mage::getModel('tokenbase/card')->getCollection();
$savedCards->addFieldToFilter('active', 1)
        ->addFieldToFilter('customer_id', $customer_id);
$card = $savedCards->getFirstItem();

$quote->getPayment()->importData(array(
    'method' => $payment_method,
    'card_id' => $card->getHash(),
));