cancel
Showing results for 
Search instead for 
Did you mean: 

Create Order Programmatically

SOLVED

Create Order Programmatically

Hi,

I want to create an order programmatically. My problem is when the qty i want is bigger than the magento stock.

The requested quantity for "xxx" is not available.

 

When quantity is avaible on stock this works well.

 

Is it possible to create order allowing more quantity than stock in magento?

- On backend i allow create orders when product stock quantity is below 0, on front end not.

 

 

$customer = Mage::getModel('customer/customer')
		->setWebsiteId($websiteId)
		->load($customer_id);

$quote = Mage::getModel('sales/quote')->setStoreId($websiteId);
$quote->assignCustomer($customer);

$_prod = Mage::getModel('catalog/product')->load(1);
$quote->addProduct($_prod,new Varien_Object(array('qty' => $_POST['qty']))); ... ... ... $quote->collectTotals()->save(); $service = Mage::getModel('sales/service_quote', $quote); $service->submitAll();

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Create Order Programmatically

@dudesjoerd, thx for your answer, i tried to set quote as supermode but now i have the next error:

Not all products are available in the requested quantity

 

I was searching and I found this:

$quote->setInventoryProcessed(true);

This works good.

View solution in original post

2 REPLIES 2

Re: Create Order Programmatically

When creating an order from the backend, the quote is set to supermode in the class Mage_Adminhtml_Model_Session_Quote (96)

 

$this->_quote->setIsSuperMode(true);

Then later, when Magento is checking the product inventory in Mage_CatalogInventory_Model_Observer in the function checkQuoteItemQty on line 303, the inventory checking is skipped if the supermode is set to true

 

if (!$quoteItem || !$quoteItem->getProductId() || !$quoteItem->getQuote()
    || $quoteItem->getQuote()->getIsSuperMode()) {
    return $this;
}

If I copy your code, and add the supermode to the quote:

 

$quote = Mage::getModel('sales/quote')->setStoreId($websiteId);
$quote->assignCustomer($customer);
$quote->setIsSuperMode(true);

then I don't get the quantity error anymore.

Did I help you solve your problem? Click Accept as Solution!
DudeSquare Web Development

Re: Create Order Programmatically

@dudesjoerd, thx for your answer, i tried to set quote as supermode but now i have the next error:

Not all products are available in the requested quantity

 

I was searching and I found this:

$quote->setInventoryProcessed(true);

This works good.