Magento Forum

Page 9 of 10
Trouble creating a quote/order through custom code (programmatically for use with API)
 
michael_g08
Sr. Member
 
Avatar
Total Posts:  108
Joined:  2009-03-06
Ukraine
 

@nanos

Hi, sorry, hadn’t receive notifications somehow......

Try this last class I’ve posted.

Regards, Michael.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nanos
Member
 
Total Posts:  35
Joined:  2009-08-06
London, UK
 

thanks Michael!!!

I’ll have a look later and report back smile

thanks for your effort anyway! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
nanos
Member
 
Total Posts:  35
Joined:  2009-08-06
London, UK
 

Unfortunately it still doesn’t work:

Here are some more details of what I am actually doing in case I do something wrong:

I have changed the file to match the following:

require_once '../app/Mage.php';
$app Mage::app();
Mage::register('isSecureArea'true); //no output before here, will get a session header error

class CustomOrder
    {
        
//your class declaration without any modification ...
    
}

$_custom_address 
= array( /*definition of address*/ );
$shoppingCart=array(
    
=> array("PartId"=>4"Quantity"=>1),
    
=> array("PartId"=>3"Quantity"=>2),
    
=> array("PartId"=>1"Quantity"=>1)
);
$paymentMethod 'purchaseorder';
$shippingMethod 'freeshipping_freeshipping';

$order = new CustomOrder();
$quoteId $order->prepareCustomerOrder(2$shoppingCart$_custom_address$_custom_address$shippingMethod) ;
$orderId $order->createOrder($quoteId$paymentMethod) ;
$order->createInvoiceForOrder($orderId);
$order->createShippingForOrder($orderId);

Can you give me any more help?

Also: what is the difference between createOrder and ConfirmOrder?

Thanks so much for your help!

nanos

 
Magento Community Magento Community
Magento Community
Magento Community
 
Zarathustra
Jr. Member
 
Total Posts:  1
Joined:  2011-01-19
 

Hi @all,

I read this thread because I have the same problem with.
Now I’ve got a question: How do I instantiate the classes from the examplefiles (guestOrders.php, MarkyCreateOrder.php, MarkyMichaelCreateOrder.php or CreateOrder.php) in my Magento-installation. I would be happy if someone could give me a small howto for usage.

Greetings from Zarathustra

PS: excuse my bad English

 
Magento Community Magento Community
Magento Community
Magento Community
 
Papiertiger
Jr. Member
 
Total Posts:  2
Joined:  2010-06-11
 

@michael_g08

thx a lot for you’re code. Comparing it to what I’ve already got, it seemed I wasn’t that wrong and i found out now, what my problem was. The observers weren’t loaded. Using the “initialize magento"-code from http://www.ecomdev.org/2010/06/01/application-bootstrap-in-magento.html the mail contains the links now. smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
medlington2
Member
 
Total Posts:  63
Joined:  2011-02-04
 

Hi,

Im using a different method but it seems to be similar, the code Im using is this:

<?php
      
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('medlington@atumstudios.com');
              
$quote->assignCustomer($customer);
      
else {

              
// for guesr orders only:
              
$quote->setCustomerEmail('customer@example.com');
      
}
       
      
// add product(s)    -    number in brackets is product no
      
$product Mage::getModel('catalog/product')->load(3);
      
$product[price] 123.00;
      
$product[special_price] 123.00;
      
$product[name] 'EDITED NAME';

     
      
$buyInfo = array(
              
'qty' => 10,
              
'discount' => 30,
              
// 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($addressData);
       
      
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                      ->
setShippingMethod('flatrate_flatrate')
                      ->
setPaymentMethod('checkmo');
       
      
$quote->getPayment()->importData(array('method' => 'checkmo'));
       
      
$quote->collectTotals()->save();
      
$service Mage::getModel('sales/service_quote'$quote);
      
$service->submitAll();
                     
//print_r ($service);
      
$order $service->getOrder();

      
printf("Created order %s\n"$order->getIncrementId());

I’ve set it so that I can override the price of the product as Im importing old orders and many of the prices have changed.

I just need to figure out a way to set the discount amount.

I’ve tried adding:

$quote[discount_amount] 777.77;

beneath:

$quote->getPayment()->importData(array(’method’ => ‘checkmo’));

but it doesnt seem to do anything,

can anyone help?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
entercompryan
Jr. Member
 
Total Posts:  16
Joined:  2011-02-04
 

I tried using the above method, but I get this error report:

a:5:{i:0;s:33:"Please specify a shipping method.";i:1;s:1345:"#0 /home/enterco2/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(284): Mage::throwException(’Please specify ...’)
#1 /home/enterco2/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(202): Mage_Sales_Model_Service_Quote->_validate()
#2 /home/enterco2/public_html/app/code/core/Mage/Sales/Model/Service/Quote.php(218): Mage_Sales_Model_Service_Quote->submitNominalItems()
#3 /home/enterco2/public_html/app/code/local/Ecorps/Quotation/controllers/Adminhtml/QuotationController.php(404): Mage_Sales_Model_Service_Quote->submitAll()
#4 /home/enterco2/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Ecorps_Quotation_Adminhtml_QuotationController->createOrderFromQuoteAction()
#5 /home/enterco2/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(253): Mage_Core_Controller_Varien_Action->dispatch(’createorderfrom...’)
#6 /home/enterco2/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#7 /home/enterco2/public_html/app/code/core/Mage/Core/Model/App.php(340): Mage_Core_Controller_Varien_Front->dispatch()
#8 /home/enterco2/public_html/app/Mage.php(627): Mage_Core_Model_App->run(Array)
#9 /home/enterco2/public_html/index.php(80): Mage::run(’’, ‘store’)
#10 {main}”;s:3:"url”;s:127:"/~enterco2/index.php/quotation/adminhtml_quotation/createorderfromquote/id/107/?isAjax=true&testvar;=a&form;_key=YsYHGjmOytev5FLN”;s:11:"script_name”;s:20:"/~enterco2/index.php”;s:4:"skin”;s:5:"admin";}

Using Magento 1.5.1.

Any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
medlington2
Member
 
Total Posts:  63
Joined:  2011-02-04
 

Not too sure,

the code I was using came from here:

http://inchoo.net/ecommerce/magento/programmatically-create-order-in-magento/

I used Vinai’s method from the comments at the bottom, maybe theres something on their that can help you.

It could always be that they’ve changed the way it works in 1.5

 
Magento Community Magento Community
Magento Community
Magento Community
 
entercompryan
Jr. Member
 
Total Posts:  16
Joined:  2011-02-04
 
medlington2 - 23 March 2011 12:17 AM

Not too sure,

the code I was using came from here:

http://inchoo.net/ecommerce/magento/programmatically-create-order-in-magento/

I used Vinai’s method from the comments at the bottom, maybe theres something on their that can help you.

It could always be that they’ve changed the way it works in 1.5

I’ve tried Inchoo and Vinai’s methods, and neither of them worked. I’m a little disappointed Magento doesn’t make this easier to do…

 
Magento Community Magento Community
Magento Community
Magento Community
 
medlington2
Member
 
Total Posts:  63
Joined:  2011-02-04
 

Just a thought, it looks like its erroring on your shipping method. The code I posted is set to use the flat rate shipping method, is that enabled on your install? if not try activating it and then run the script again.

I often wonder why what seems like quite simple but very usefull features like this arnt nativly in Magento but then I see they have a paid version which probably does have all these features and then it makes sense. Luckily there always seems a way to hack it to get what you want

 
Magento Community Magento Community
Magento Community
Magento Community
 
Nishant Gauttam
Jr. Member
 
Avatar
Total Posts:  8
Joined:  2010-12-28
 

Hi guyz,

Thanks for your efforts in the thread, because of which I was able to create custom orders. But My requirement is a bit different.
I need to import order history and lot of products are inactive now.
1) so in that case I will not be able to provide product Id or SKU to create product object to be sent in prepare order function.
2) My Shipping rates have changed, If I use the table rates it starts showing new shipping rates in Previous Orders and change the total order amount as well. There might be case where for a particular customer I might have given a special discount so I want to set the total amount of order as well through the code instead of simply calling collectTotals function and let the magento set everything itself.

how can I set the discount, shipping rate , total amount , order creation date?

 
Magento Community Magento Community
Magento Community
Magento Community
 
medlington2
Member
 
Total Posts:  63
Joined:  2011-02-04
 

For products that no longer exist and for ones sold at different rates I’ve overwritten the info in the array after its been called. In my code above these lines overwrite the info pulled from the Magento product:

// add product(s)    -    number in brackets is product no
      $product Mage::getModel('catalog/product')->load(3);
      
$product[price] 123.00;
      
$product[special_price] 123.00;
      
$product[name] 'EDITED NAME';

Maybe need to seup a blank default product that gets called instead of a live one

 
Magento Community Magento Community
Magento Community
Magento Community
 
PaulMarclay
Jr. Member
 
Total Posts:  1
Joined:  2010-07-02
 

Jesse_dev,
I’ve noticed that when you make a sale like this, the stock of products is not reduced, any idea why this happens?

Thanks.

jesse_dev - 09 May 2009 05:53 PM

I’m not receiving email from this thread for some reason, sorry for the delay.
i just checked the email notification so i should get notified of every post on here now.

@faisal - the params array is basically an array of product ids and quantities, like a shopping cart

it looks like this

$shopping_cart=array();

$part=array("PartId"=>x"Quantity"=>y);
$shopping_cart[]=$part//repeat as necessary

$params=array("AccountNo"=>$customer_id"PartCart"=>$shopping_cart);


@freshwebservices - the code you sent me in msg is quite a bit different than what you’re describing here. the code works as is. any small change can break it.
 
Magento Community Magento Community
Magento Community
Magento Community
 
rosh3000
Jr. Member
 
Total Posts:  6
Joined:  2011-06-07
 

Thank you jesse_dev, solution worked in version 1.6
(did need modifying as its part of ebay integration and need to set customers as guest)

jesse_dev - 13 April 2009 10:54 AM

here’s some code for creating an order ‘outside of magento’
for example, i have a custom soap implementation in /soap/
setting a payment method in the quote before you save() seems to cause errors
i’ve been working on this for about a week..

require_once '../app/Mage.php';
    
$app Mage::app();
    
Mage::register('isSecureArea'true);
    
$customerObj=Mage::getModel('customer/customer')->load($AccountNo);
    
$quoteObj=Mage::getModel('sales/quote')->assignCustomer($customerObj);
    
$quoteObj->reserveOrderId();
    
    
$productModel=Mage::getModel('catalog/product');
    foreach(
$PartCart as $part{
        
foreach($part as $k=>$v$$k=$v}
        $productObj
=$productModel->load($PartId);
        
$quoteObj->addProduct($productObj);
    
}
    $quoteObj
->collectTotals();
    
$quoteObj->save();

//$quoteId=$quoteObj->getId();
//$quoteObj = Mage::getModel('sales/quote')->load($quoteId);
//$orderCreateModel=Mage::getSingleton('adminhtml/sales_order_create');

    
$quotePaymentObj=$quoteObj->getPayment();
    
//methods: authorizenet, paypal_express, googlecheckout, purchaseorder
    
$quotePaymentObj->setMethod('purchaseorder');
    
$quoteObj->setPayment($quotePaymentObj);
    
$convertQuoteObj=Mage::getSingleton('sales/convert_quote');
    
$orderObj $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());
    
$orderPaymentObj=$convertQuoteObj->paymentToOrderPayment($quotePaymentObj);

    
$orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));
    
$orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));
    
$orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));
    
    
$qty=1;
    foreach (
$quoteObj->getShippingAddress()->getAllItems() as $item{
        
//@var $item Mage_Sales_Model_Quote_Item
        
$item->setQty($qty);
        
$orderItem $convertQuoteObj->itemToOrderItem($item);
        if (
$item->getParentItem()) {
            $orderItem
->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId()));
        
}
        $orderObj
->addItem($orderItem);
    
}
    
    $orderObj
->setCanShipPartiallyItem(false);
    
    
$totalDue=$orderObj->getTotalDue();
    echo 
"<p>total due: $totalDue</p>";
    
$orderObj->place(); //calls _placePayment
    
$orderObj->save();
    
$orderId=$orderObj->getId();
    echo 
"<p>orderId: $orderId</p>";
 
Magento Community Magento Community
Magento Community
Magento Community
 
cac3a
Jr. Member
 
Total Posts:  13
Joined:  2010-06-14
 

The last post by rosh3000 works fine when creating one order at a time. I tried using it with csv input file to import orders, but the items are carried over from one order to another. For example, first and second order has 1 item/order. The second order will contain both items from 1st order and second order. Any ideas on how to fix that problem ?
I basically have the order creation function wrapped in a loop that iterates over a csv file and gathers the data. I’m thinking that I will have to dispatch an action with each iteration...hmmm?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 9 of 10