Trying to import orders to magento from other platform. I have almost done except import products with attribute stock_data is_in_stock set to 0. If I set manually the value to 1 imports correctly. I've tried it setting the value temporally to 1 and once is imported set it to 0 when needed, but i think is not the best way. Is there any other way to do it? any help will be appreciated.
function _order_create($data){
if(is_array($data) and !empty($data['product'])){
$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore('default')->getId());
$customer = Mage::getModel('customer/customer')
->setWebsiteId(1)
->loadByEmail($data['customer_email']);
$quote->assignCustomer($customer);
// add product(s)
foreach ($data['product'] as $id => $pdata) {
$product = Mage::getModel('catalog/product')->load($pdata['id']);
$buyInfo = array('qty' =>$pdata['qty']);
//****************** here set is_in_stock to 1
$quote->addProduct($product, new Varien_Object($buyInfo));
$quote->save(); // without saving the quote here I get error
$quoteItem = $quote->getItemByProduct($product);
$quoteItem->setOriginalCustomPrice($pdata['price']);
$quoteItem->setCustomPrice($pdata['price']);
$quoteItem->setIsSuperMode(true);
$quoteItem->save();
// ****************** here set is_in_stock to 0
}
foreach ($data['address']['shipping'] as $key => $value) {
$addressDataShipping[$key] = $value;
}
foreach ($data['address']['billing'] as $key => $value) {
$addressDataBilling[$key] = $value;
}
$billingAddress = $quote->getBillingAddress()->addData($addressDataBilling);
$shippingAddress = $quote->getShippingAddress()->addData($addressDataShipping);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod($data['methods']['shipping'])
->setPaymentMethod($data['methods']['payment']);
$quote->getPayment()->importData(array('method' => $data['methods']['payment']));
$quote->setTotalsCollectedFlag(false)->collectTotals()->save();
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();
$order->setCreatedAt($data['order']['createdat']);
$order->setData('state', $data['order']['status']);
$order->setStatus($data['order']['status']);
$order->save();
//printf("Created order %s\n", $order->getIncrementId());
}
}
This is the method im using
Thanks for reading!
EDIT: the easiest way is before start adding products set manage_stock to 0 and once finished set it back to 1