genearate order programatically magento , along with sales quotes, sales items and sales address 10 comments


Complete code with customer registered , genearate order programatically,
along with sales quotes, sales items and sales address
<?php
//http://www.magentocommerce.com/boards/viewthread/28426/P45/
require_once '../app/Mage.php';
$app = Mage::app();
Mage::register('isSecureArea', true); //no output before here, will get a session header error

$customer_id = 222; //222 rmuthe

$shopping_cart = array();

/*
  $part = array(
  array("PartId" => '1', "Quantity" => '1'),
  array("PartId" => '2', "Quantity" => '1')
  );
 */
$part = array("PartId" => '1', "Quantity" => '2');
$part1 = array("PartId" => '2', "Quantity" => '2');
//$part= array();


$shopping_cart[] = $part; //repeat as necessary
$shopping_cart[] = $part1; //repeat as necessary


$params = array("AccountNo" => $customer_id, "PartCart" => $shopping_cart);
$quote_pk = PrepareOrder($params);
$order_pk = ConfirmOrder($quote_pk);


echo "<br />Quote Id Generated : " . $quote_pk;
echo "<br />Order Id Generated : " . $order_pk;


echo "<br />DONE";

function PrepareOrder($params) {
    foreach ($params as $k => $v) {
        $$k = $v;
    }
    $customerObj = Mage::getModel('customer/customer')->load($AccountNo);
    $storeId = $customerObj->getStoreId();
    $quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj); //sets ship/bill address
    $storeObj = $quoteObj->getStore()->load($storeId);
    $quoteObj->setStore($storeObj);
    $productModel = Mage::getModel('catalog/product');

    foreach ($PartCart as $part) {
        foreach ($part as $k => $v) {
            $$k = $v;
        }
        $productObj = $productModel->load($PartId);

        //Modified Here annet-pk
        //$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
        try {
            $quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
        } catch (Exception $e) {
            echo $e;
        }
        $quoteItem->setQuote($quoteObj);
        $quoteItem->setQty($Quantity);
        $quoteObj->addItem($quoteItem);
    }


    /*
    $quoteObj->collectTotals();
    $quoteObj->save();
    */
    $shippingMethod = 'flatrate_flatrate';
    $quoteObj->getShippingAddress()->setShippingMethod($shippingMethod);
    $quoteObj->getShippingAddress()->setCollectShippingRates(true);
    $quoteObj->getShippingAddress()->collectShippingRates();
    $quoteObj->collectTotals(); // calls $address->collectTotals();
    $quoteObj->save();

    $quoteId = $quoteObj->getId();
    return $quoteId;
}

function ConfirmOrder($quoteId) {

    $hpc_connector_orderid = '786-2222222-3333333';
    $hpc_connector_sitename = 'ebay'; //ebay / amazon
    //methods: authorizenet, paypal_express, googlecheckout, purchaseorder
    $hpc_payment_method = 'checkmo';

    //methods: flatrate_flatrate, freeshipping_freeshipping
    $hpc_shipping_method = 'flatrate_flatrate';
    $hpc_shipping_method_description = 'eBay Order Fullfilment';



    $quoteObj = Mage::getModel('sales/quote')->load($quoteId);
    $items = $quoteObj->getAllItems();
    $quoteObj->collectTotals();
    $quoteObj->reserveOrderId();



    $quotePaymentObj = $quoteObj->getPayment();
    //methods: authorizenet, paypal_express, googlecheckout, purchaseorder
    $quotePaymentObj->setMethod($hpc_payment_method);

    $quoteObj->setPayment($quotePaymentObj);
    $convertQuoteObj = Mage::getSingleton('sales/convert_quote');


    $orderObj = $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());



    $orderPaymentObj = $convertQuoteObj->paymentToOrderPayment($quotePaymentObj);

    $orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));

    //annet -pk to set shipping method
    // $orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));
    $orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()))
            ->setShipping_method($hpc_shipping_method)
            ->setShippingDescription($hpc_shipping_method_description);

    $orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));

    $orderObj->setHpcOrderId($hpc_connector_orderid);
    $orderObj->setHpcOrderFrom($hpc_connector_sitename);

    foreach ($items as $item) {
        //@var $item Mage_Sales_Model_Quote_Item
        $orderItem = $convertQuoteObj->itemToOrderItem($item);
        if ($item->getParentItem()) {
            $orderItem->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId()));
        }
        $orderObj->addItem($orderItem);
    }
    $orderObj->setCanShipPartiallyItem(false);

    $totalDue = $orderObj->getTotalDue();

     //$orderObj->sendNewOrderEmail();

    $orderObj->place(); //calls _placePayment
    $orderObj->save();


    //  $orderId = $orderObj->getId();
    //   return $orderId;




    $orderObj->load(Mage::getSingleton('sales/order')->getLastOrderId());
    $lastOrderId = $orderObj->getIncrementId();
    return $lastOrderId;
    echo "Recent Order Id :".$lastOrderId;


}


?>

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools


10 thoughts on “genearate order programatically magento , along with sales quotes, sales items and sales address