0

I am facing issues while creating sales order programmatically.

Here is my code :-

require_once 'app/Mage.php';
umask(0);
Mage::app('default');

$store = Mage::app()->getStore();
$website = Mage::app()->getWebsite();

//Create sales quote object
$quote = Mage::getModel('sales/quote')->setStoreId($store->getStoreId());

//Customer information
$customerEmail = "diegodelgado900@msn.com";
$customerFirstname = "Diego"; 
$customerLastname = "Delgado";

$billingAddress = array(
    'customer_address_id' => '',
    'prefix' => '',
    'firstname' => $customerFirstname,
    'middlename' => '',
    'lastname' => $customerLastname,
    'suffix' => '',
    'company' => '', 
    'street' => array(
        '0' => '13877 sw 151 lane'),
    'city' => 'Miami',
    'country_id' => 'US', // country code
    'region' => 'Miami',
    'region_id' => '2',
    'postcode' => '33186',
    'telephone' => '999-888-0000',
    'fax' => '',
    'save_in_address_book' => 1
);

$shippingAddress = array(
    'customer_address_id' => '',
    'prefix' => '',
    'firstname' => $customerFirstname,
    'middlename' => '',
    'lastname' => $customerLastname,
    'suffix' => '',
    'company' => '', 
    'street' => array(
        '0' => '13877 sw 151 lane'),
    'city' => 'Miami',
    'country_id' => 'US', // country code
    'region' => 'Miami',
    'region_id' => '2',
    'postcode' => '33186',
    'telephone' => '999-888-0000',
    'fax' => '',
    'save_in_address_book' => 1
);

//Check whether the customer already registered or not
$customer = Mage::getModel('customer/customer')->setWebsiteId($website->getId())->loadByEmail($customerEmail);

if (!$customer->getId()) {

    //Create the new customer account if not registered
    $customer = Mage::getModel('customer/customer'); 
    $customer->setWebsiteId($website->getId())
             ->setStore($store)
             ->setFirstname($customerFirstname)
             ->setLastname($customerLastname)
             ->setEmail($customerEmail);

    try {
        $password = $customer->generatePassword(); 
        $customer->setPassword($password);

        //Set the customer as confirmed
        $customer->setForceConfirmed(true);
        $customer->save();

        $customer->setConfirmation(null);
        $customer->save();

        //Set customer address
        $customerId = $customer->getId(); 
        $customAddress = Mage::getModel('customer/address'); 
        $customAddress->setData($billingAddress)
                      ->setCustomerId($customerId)
                      ->setIsDefaultBilling('1')
                      ->setIsDefaultShipping('1')
                      ->setSaveInAddressBook('1');

        //Save customer address
        $customAddress->save();

        //Send new account email to customer
        $storeId = $customer->getSendemailStoreId();
        $customer->sendNewAccountEmail('registered', '', $storeId);

        //Set password remainder email if the password is auto generated by magento
        $customer->sendPasswordReminderEmail();

    } catch (Exception $e) {
        Mage::logException($e);
    } 
}

//Assign the customer to quote
$quote->assignCustomer($customer);

//Set currency for the quote
$quote->setCurrency(Mage::app()->getStore()->getBaseCurrencyCode());

$productIds = array(14470,1); //array('product_id' => 'qty')

//Add products to quote
foreach($productIds as $productId => $qty) {
    $product = Mage::getModel('catalog/product')->load($productId);
    $quote->addProduct($product, $qty);
}

//Add billing address to quote
$billingAddressData = $quote->getBillingAddress()->addData($billingAddress);

//Add shipping address to quote
$shippingAddressData = $quote->getShippingAddress()->addData($shippingAddress);

//Collect shipping rates on quote
$shippingAddressData->setCollectShippingRates(true)->collectShippingRates();

//Set shipping method and payment method on the quote
$shippingAddressData->setShippingMethod('freeshipping_freeshipping')->setPaymentMethod('free'); //Shipping is flatrate for this example

//Set payment method for the quote
$quote->getPayment()->importData(array('method' => 'free')); //Payment is check and money order for this example

try {
    //Collect totals & save quote
    $quote->collectTotals()->save();

    //Create order from quote
    $service = Mage::getModel('sales/service_quote', $quote);
    $service->submitAll();
    $increment_id = $service->getOrder()->getRealOrderId();

    echo 'Order Id: ' .$increment_id. ' has been successfully created.';

} catch (Exception $e) {
    Mage::logException($e);
}

Error :-

Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'This product is currently out of stock.' in /home/domain/public_html/app/Mage.php:598 Stack trace: #0 /home/domain/public_html/app/code/core/Mage/Sales/Model/Quote.php(1022): Mage::throwException('This product is...') #1 /home/domain/public_html/app/code/core/Mage/Sales/Model/Quote.php(1046): Mage_Sales_Model_Quote->addProductAdvanced(Object(Anowave_Ec_Model_Product), 1, 'full') #2 /home/domain/public_html/test.php(116): Mage_Sales_Model_Quote->addProduct(Object(Anowave_Ec_Model_Product), 1) #3 {main} thrown in /home/domain/public_html/app/Mage.php on line 598

Here is stock of product

This error comes up also when i change the products.

Also i have clear the cache and re-index all tried, But no luck.

Please help.

0

Its looked like your product is Out Of Stock check the product in admin and assign some quantity and make the stock status to in stock.

enter image description here

  • It is in stock, please see the image in original question that show stock quantity. – Balkishan Natani Jan 25 '19 at 7:37
  • @BalkishanNatani, yes that is fine you have the stock quantity but can you check the stock status is in-stock or out of stock – Amit Naraniwal Jan 25 '19 at 8:44
  • I checked ...its in stock here...is there is any other way to generate sales order? – Balkishan Natani Jan 26 '19 at 6:41
  • @BalkishanNatani, Yes you can do by allowing backorder. I am adding screen for allowing backorder – Amit Naraniwal Jan 28 '19 at 6:22
  • see I have added the backorder screen. – Amit Naraniwal Jan 28 '19 at 6:23

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.