0

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

0

Are you using the manage stock feature? You could turn this off whilst the import runs? Or configure backorders? Even just temporarily for the product you are importing. Or use your approach. Your approach is the simplest I can think of.

  • Thanks @dominic-xigen disabling manage_stock and enable it after the import is the easiest way to do it. Otherwise i need to know the previous 'is_in_stock' value to set it back. – rekobeko May 24 '19 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.