Magento Forum

Trouble creating a quote/order through custom code (programmatically for use with API)
 
BVBAccelerate
Member
 
Avatar
Total Posts:  73
Joined:  2009-05-19
Rochester, NY
 

Hey Michael,

I’m using your script to create orders programatically in Magento. However, I can not get it to work past the first line in preparing a guest order

$quoteObj = Mage::getModel(’sales/quote’);

is generating:

PHP Fatal error: Call to a member function getModelInstance() on a non-object in /var/www/vhosts/smokefrii.com/httpdocs/store/app/Mage.php on line 432

At the top of your class file I put: require_once ‘../store/app/Mage.php’;

I figured this would be what I needed to run these scripts. Is there something that I’m missing. I’m sure your code isn’t totally wrong here but I’m trying to create these orders programatically from outside Magento and obviously must be missing a vital part.

Any help you can provide us much appreciated, thanks!

SOLUTION:
At the top of Michael’s CustomOrder class I added these lines:
require_once ‘../path/to/Mage.php’;
umask(0);
Mage::app(’default’);

 
Magento Community Magento Community
Magento Community
Magento Community
 
BVBAccelerate
Member
 
Avatar
Total Posts:  73
Joined:  2009-05-19
Rochester, NY
 

Okay guys,

Michael’s CustomOrder class has worked for me as far as getting the order, invoice, and shipment into Magento. I am using the free shipping method and authorize.net as my payment method.

The order is showing up with its status blank in the backend though. It is not capturing the payment either. When I view an invoice for an order I try to click the button to capture it but it says that there is no credit card information for the order. It is saving the last 4 numbers of the credit card so I know that I am putting it in correctly.

Is this designed to actually capture the credit card information and cause a card to be charged, or is this not capable of that?

If anyone has any experience with this I’d like your advice before I go diving into the code and trying to research from scratch.

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
BVBAccelerate
Member
 
Avatar
Total Posts:  73
Joined:  2009-05-19
Rochester, NY
 

I’m lighting up this post.

I was also wondering if anyone had any ideas about how to add custom options to the products in the order being programmatically created.

I’ve looked into the methods for adding custom options on the products and also on the quote items. I can’t seem to figure out which one is the right one to use, or the correct format for entering them in there. I’ve found these methods in the model files for Quote:Item and Catalog:Product.

If anyone has any ideas or experience with this It would really help me out. Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
michael_g08
Sr. Member
 
Avatar
Total Posts:  108
Joined:  2009-03-06
Ukraine
 

About athorize.net, SagePay, PayPal and company…

These all methods use CC.php payment method. It does not saves CC data.
Want to save CC data to DB? Use CCSave method instead or just change a few
strings in CC.php. You will see what I mean when look into it.

You can retrieve saved data from quote payment method:

$paymentMethod = $quote->getPayment();
$CcNumber = Mage::helper(’core’)->decrypt($paymentMethod->getCcNumberEnc());
end ....

Michael.

 
Magento Community Magento Community
Magento Community
Magento Community
 
RobertoArtV
Jr. Member
 
Total Posts:  2
Joined:  2009-11-22
 

Hi!

First of all, thanks a lot to all of you. This thread is awesome!

OK, I’ve been able to create customers and orders thanks to various snippets of code found here, but I’d like one last thing.

Right now, the order gets the shipping costs from the store with

$quoteObj->getShippingAddress()->setShippingMethod('tablerate_bestway');
            
$quoteObj->getShippingAddress()->setCollectShippingRates(true)->save();

Is there a way to set completely different shipping costs from the quote or order creation? The cost would be always the same, so I can enter the number directly… but how?

Thanks a lot!

 
Magento Community Magento Community
Magento Community
Magento Community
 
faisal2010
Jr. Member
 
Avatar
Total Posts:  21
Joined:  2009-02-03
Karachi
 

Hi Roberto,

You can use Flat Rate shipping method and set the shipping rates in configuration call in the code like

$quoteObj->getShippingAddress()->setShippingMethod('flatrate_flatrate');
            
$quoteObj->getShippingAddress()->setCollectShippingRates(true)->save();

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
mohamed.mechergui@whitecapetech.com
Jr. Member
 
Total Posts:  6
Joined:  2010-09-07
 

good morning my ppl !!!

i got a problem with programming an order with magento code !!
i have the order and the invoice saved in the database but downloadable products are not stored in the costumer’s downloadable products page !!
do any bodu have any idea about the reason ????

thx

 
Magento Community Magento Community
Magento Community
Magento Community
 
zzzmago
Jr. Member
 
Total Posts:  2
Joined:  2010-08-26
 

Hi
I’m trying to use code posted by marky, but I get error. Below my input data and error log
Pleas help. Input data is correct ?

$email 'myemail@domain.com';
        
$shoppingCart = array(array('PartId'=> 178'Quantity' => 1));
        
$shippingAddress = array(
        
'firstname'  => 'Juan',
        
'lastname'   => 'Perez',
        
'country_id' => 'PL',
        
'region_id'  => '',
        
'region'     => 'Santiago',
        
'city'       => 'Santiago Centro',
        
'street'     => array('Marin 444','Depto. 5434'),
        
'telephone'  => '765434',
        
'postcode'   => '00-000',
        
'save_in_address_book' => 0,
     
        
'is_default_billing'  => false,
        
'is_default_shipping' => true,        
        
         
'prefix'               => '',            
         
'middlename'           => '',           
         
'suffix'               => '',
         
'company'               => '',            
         
'fax'                   => ''
        
); 
        
$billingAddress = array(
        
'firstname'  => 'Mario',
        
'lastname'   => 'Mora',
        
'country_id' => 'PL',
        
'region_id'  => '',
        
'region'     => 'Santiago',
        
'city'       => 'Providencia',
        
'street'     => array('Santa Lora 75','Oficina 13801'),
        
'telephone'  => '654325',
        
'postcode'   => '00-000',
        
'save_in_address_book' => 0,
     
        
'is_default_billing'  => true,
        
'is_default_shipping' => false,
        
'prefix'               => '',
            
        
'middlename'           => '',           
        
'suffix'               => '',
        
'company'               => '',            
        
'fax'                   => ''
        
);
        
$shippingMethod 'flatrate_flatrate';
        
$couponCode '';

a:5:{i:0;s:110:"Notice: Array to string conversion in C:\Program Files\Zend\magento\lib\Zend\Db\Statement\Pdo.php on line 228";i:1;s:2644:"#0 [internal function]: mageCoreErrorHandler(8, ‘Array to string...’, ‘C:\Program File...’, 228, Array)
#1 C:\Program Files\Zend\magento\lib\Zend\Db\Statement\Pdo.php(228): PDOStatement->execute(Array)
#2 C:\Program Files\Zend\magento\lib\Zend\Db\Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#3 C:\Program Files\Zend\magento\lib\Zend\Db\Adapter\Abstract.php(468): Zend_Db_Statement->execute(Array)
#4 C:\Program Files\Zend\magento\lib\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query(’INSERT INTO `sa...’, Array)
#5 C:\Program Files\Zend\magento\lib\Varien\Db\Adapter\Pdo\Mysql.php(333): Zend_Db_Adapter_Pdo_Abstract->query(’INSERT INTO `sa...’, Array)
#6 C:\Program Files\Zend\magento\lib\Zend\Db\Adapter\Abstract.php(546): Varien_Db_Adapter_Pdo_Mysql->query(’INSERT INTO `sa...’, Array)
#7 C:\Program Files\Zend\magento\app\code\core\Mage\Core\Model\Mysql4\Abstract.php(390): Zend_Db_Adapter_Abstract->insert(’sales_flat_quot...’, Array)
#8 C:\Program Files\Zend\magento\app\code\core\Mage\Core\Model\Abstract.php(284): Mage_Core_Model_Mysql4_Abstract->save(Object(Mage_Sales_Model_Quote_Address))
#9 C:\Program Files\Zend\magento\app\code\core\Mage\Core\Model\Mysql4\Collection\Abstract.php(222): Mage_Core_Model_Abstract->save()
#10 C:\Program Files\Zend\magento\app\code\core\Mage\Sales\Model\Quote.php(201): Mage_Core_Model_Mysql4_Collection_Abstract->save()
#11 C:\Program Files\Zend\magento\app\code\core\Mage\Core\Model\Abstract.php(285): Mage_Sales_Model_Quote->_afterSave()
#12 C:\Program Files\Zend\magento\app\code\local\Mage\Transakcje\Model\Order.php(109): Mage_Core_Model_Abstract->save()
#13 C:\Program Files\Zend\magento\app\code\local\Mage\Transakcje\controllers\TransakcjeController.php(66): Mage_Transakcje_Model_Order->prepareGuestOrder(’myemail@domain....’, Array, Array, Array, ‘flatrate_flatra...’, NULL)
#14 C:\Program Files\Zend\magento\app\code\core\Mage\Core\Controller\Varien\Action.php(418): Mage_Transakcje_TransakcjeController->testAction()
#15 C:\Program Files\Zend\magento\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch(’test’)
#16 C:\Program Files\Zend\magento\app\code\core\Mage\Core\Controller\Varien\Front.php(177): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#17 C:\Program Files\Zend\magento\app\code\core\Mage\Core\Model\App.php(304): Mage_Core_Controller_Varien_Front->dispatch()
#18 C:\Program Files\Zend\magento\app\Mage.php(599): Mage_Core_Model_App->run(Array)
#19 C:\Program Files\Zend\magento\index.php(78): Mage::run(’’, ‘website’)
#20 {main}”;s:3:"url”;s:45:"/Magento/index.php/transakcje/transakcje/test”;s:11:"script_name”;s:18:"/Magento/index.php”;s:4:"skin”;s:7:"default";}

 
Magento Community Magento Community
Magento Community
Magento Community
 
jpk.in
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2009-08-12
Cochin, India
 

Thanks for the post.. really helpful.

But i am getting an error like below when trying to create order.. The quote id is generated without any problem..

SQLSTATE[23000]: Integrity constraint violation: 1048 Column \’store_to_base_rate\’ cannot be null…

Any idea why?

 
Magento Community Magento Community
Magento Community
Magento Community
 
bilaco
Jr. Member
 
Total Posts:  8
Joined:  2009-03-02
 

in magento 1.3 i used the jesse_dev code and works great, but not in 1.4 version
i tested codes from alkarim, marky and michael_g08, but none changed the product inventory

can someone help me?

 
Magento Community Magento Community
Magento Community
Magento Community
 
bilaco
Jr. Member
 
Total Posts:  8
Joined:  2009-03-02
 

I change jesse_dev code and now this works in magento 1.4 wink

public function orderAction() {
        $post 
$this->getRequest()->getPost();

        
$shoppingCart = array();
        for (
$i 0$i<count($post['product_id']); $i++) {
            $productId 
$post['product_id'][$i];
            
$productQty $post['product_qty'][$i];
            
$product Mage::getModel('catalog/product')->load($productId);
            
$shoppingCart[] = array('product'=>$product'productQty'=>$productQty);
        
}
        $customer 
Mage::getModel('customer/customer')->load($post['customer_id']);
        
$params=array('customer'=>$customer'shoppingCart'=>$shoppingCart,);

        
$quote $this->_prepareOrder($params);
        
$order $this->_confirmOrder($quote);
        
$this->_invoiceOrder($order);

        
$this->indexAction();

    
}

    
protected function _prepareOrder($params{
        
foreach($params as $k=>$v$$k=$v}
        $storeId 
$customer->getStoreId();

        
$quote Mage::getModel('sales/quote')->assignCustomer($customer); //sets ship/bill address
        
$quote->getBillingAddress()->setShouldIgnoreValidation(true);
        
$quote->getShippingAddress()->setShouldIgnoreValidation(true);

        
$store $quote->getStore()->load($storeId);
        
$quote->setStore($store);

        foreach(
$shoppingCart as $shoppingItem{
            
foreach($shoppingItem as $k=>$v$$k=$v}
            $quoteItem
=Mage::getModel('sales/quote_item')->setProduct($product);
            
$quoteItem->setQuote($quote);
            
$quoteItem->setQty($productQty);
            
$quote->addItem($quoteItem);
        
}

        $shippingAddress 
$quote->getShippingAddress();
        
$shippingAddress->setShippingMethod('freeshipping');
        
$rate = new Mage_Sales_Model_Quote_Address_Rate();
        
$rate->setCode('freeshipping');
        
$shippingAddress->addShippingRate($rate);
        
$shippingAddress->setCollectShippingRates(true);
                
        
$quote->getPayment()->setMethod('purchaseorder');
        
$quote->collectTotals()->save();

        return 
$quote;
    
}

    
// code from Onepage.php (Mage_Checkout_Model_Type_Onepage)
    
protected function _confirmOrder($quote{
        $service 
Mage::getModel('sales/service_quote'$quote);
        
$service->submitAll();
        
Mage::getSingleton('checkout/session')->setLastQuoteId($quote->getId())
            ->
setLastSuccessQuoteId($quote->getId())
            ->
clearHelperData();
        return 
$service->getOrder();
    
}

    
protected function _invoiceOrder($order{
        $convertOrder
=Mage::getSingleton('sales/convert_order');
        
$invoice=$convertOrder->toInvoice($order);

        foreach (
$order->getAllItems() as $item{
            $invoiceItem 
$convertOrder->itemToInvoiceItem($item);
            if (
$item->getParentItem()) {
                $invoiceItem
->setParentItem($invoice->getItemById($item->getParentItem()->getId()));
            
}
            $invoiceItem
->setQty($item->getQtyToInvoice());
            
$invoice->addItem($invoiceItem);
        
}

        $invoice
->collectTotals();
        
$invoice->register();

        
$orderPayment=$order->getPayment();
        
$orderPayment->pay($invoice);

        
// workaround for magento 1.4
        
$invoice->getOrder()->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_SHIPfalse);
        
$order $invoice->getOrder();
        
$order->setData('state'Mage_Sales_Model_Order::STATE_COMPLETE);
        
$status $order->getConfig()->getStateDefaultStatus(Mage_Sales_Model_Order::STATE_COMPLETE);
        
$order->addStatusToHistory($status);

        
$transaction Mage::getModel('core/resource_transaction');
        
$transaction->addObject($invoice);
        
$transaction->addObject($invoice->getOrder());
        
$transaction->save();

        
$invoice->save();

        return 
$invoice;
    
}

i need to set discount percent
can someone help me ?

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

Hi everybody!

First of all: Thank you so much for your work and effort! It\’s greatly appreciated!

I do use Michael\’s solution as posted in #104, and it works rather well in Version 1.4.1. I do however have one problem: The inventory isn\’t updated.

Can someone help me? That would be great!

regards
nanos smile

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

Can’t anybody help me? I would really be very happy to able to use this.

Thanks ind advance smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Papiertiger
Jr. Member
 
Total Posts:  2
Joined:  2010-06-11
 
mohamed.mechergui@whitecapetech.com - 01 November 2010 04:42 AM

...but downloadable products are not stored in the costumer’s downloadable products

First off all: Thx for all the solutions in this thread. This really helped a lot to get the order process almost work. But ordering downloadable products, I’ve got the same problem as some others before. The download links are not created and don’t show up in the order email. After some time debugging and reading the code I still can’t get it work. May be someone alread has a solution to that?

 
Magento Community Magento Community
Magento Community
Magento Community
 
michael_g08
Sr. Member
 
Avatar
Total Posts:  108
Joined:  2009-03-06
Ukraine
 

@Papiertiger

Hi, take a look at this code. It works for downloadable, and bundle products on my 1.4.1

Best wishes, Michael.

File Attachments
CreateOrder.php  (File Size: 14KB - Downloads: 430)
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top