Create Order and Invoice using Magento

Create Order and Invoice using Magento

In this Post We Will Explain About is Create Order and Invoice using Magento With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to create order invoice programmatically in Magento Example

In this post we will show you Best way to implement How To Create Transaction In Magento2 Programmatically, hear for create order programmatically customer as guest in magento with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Step 1 : set Store Id

1 First off all We need to set Store Id by changing the source code as below simple source code:

/* Set Store Id */
$dataStoreId = 2; // Here 2 is some Store Id
$infoStore = Mage::getModel('core/store')->load($dataStoreId);

Step 2 : user information / address

2 Set or assigned your user information / address and make user if not any user exist. Do this as the simple step to following source code:


// Set User Information /Address
$productId = 989825; // Magento Product Id
$user_email = “[email protected]”; // User Email Address
$user_fname = “jaydeep Gondaliya”; 
$user_lname = “Gondaliya”;
$street = “USA Rajkot 360002”;
$city = USA;
$region = “Region”;
$postcode = “123589”;
$mobile = “9898225074”;
$country_id = “+61”;
$user_ip =“121.202.192.168”;

// Check user by website id
$user = Mage::getModel("user/user");
$user->setWebsiteId($infoStore->getWebsiteId())->loadByEmail($user_email);

// If store some data of website not found,so here set admin store id
if(count($user->getData())setWebsiteId(Mage_Core_Model_App::ADMIN_STORE_ID)->loadByEmail($user_email);
}

if(count($user->getData())setGroupId(1);
    $user->setEmail($user_email);
    $user->setFirstname($user_fname);
    $user->setLastname($user_lname);
    $user->save();
}


Step 3: sales quote model to make sales order

here simple condition some sales dataQuote make model to make some sales order. display the some source code below:

$dataQuote = Mage::getModel('sales/dataQuote')->setStoreId($dataStoreId);

Step 4 : Assign customer to the quote

Assign user to the dataQuote as the following simple source code:


if(count($user->getData())>0){
    // For some Existing User
    $dataQuote->assignUser($user);
}else{
    // For Guest Orders Only
    $dataQuote->setUserEmail($user_email);


Step 5 : billing/shipping address,

Last and Most important step which will some magento execute the final orcer process. From this Last step We will get simple assigned crate new product, assigned simple way to billing/shipping address like as a below some code, assigned simple or best way to payment method, maked a simple data customer order and liveInvoice. Display the source code below:


// Get Product data from Id							
$_product = Mage::getModel('catalog/product')->setStoreId($dataStoreId)->load($productId);

if(count($_product->getData())>0){
    // Assign product(s) to qoute
    $infoBuy = array('qty'=>1,'product'=>$_product->getId());
    $dataQuote->addProduct($_product,new Varien_Object($infoBuy));
    
    /* Check simple region for country */
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    //Select Query
    $query = $write->query("SELECT * FROM `directory_country_region` WHERE `country_id`='".$country_id."'");
    while($contry_data=$query->fetch()){
        $has_country_region=true;
        break;
    }
    if($has_country_region){
        $addressData = array(
            'firstname' => $user_fname, //firstname
            'lastname' => $user_lname, //lastname
            'street' => $street,  //streetname
            'city' => $city,   //cityname
            'region_id' =>$region,
            'postcode' => $postcode, //69555
            'mobile' => $mobile,  //9898526522
            'country_id' => $country_id  //+1
        );
    }else{
        $addressData = array(
            'firstname' => $user_fname, //mayur
            'lastname' => $user_lname,  // dhameliya
            'street' => $street,
            'city' => $city, //city name
            'region' =>$region, // region name
            'postcode' => $postcode, // 360002
            'mobile' => $mobile,  //9898225052
            'country_id' => $country_id //+91
        );
    }
    
    // Add Billing Address to qoute
    $billingAddress = $dataQuote->getBillingAddress()->addData($addressData);
    
    // Assign payment method i.e Check or Money Order
    $dataQuote->getPayment()->importData(array('method' => 'checkmo'));
    // Save dataQuote data
    $dataQuote->collectTotals()->save();
    
    $service = Mage::getModel('sales/service_dataQuote', $dataQuote);
    $service->submitAll();
    $order = $service->getOrder();
    
    if($user_ip){
        //Now set newly entered order's remote IP.
        $order->setRemoteIp($user_ip);
        
        //Finally we save our order after setting it's IP.
        $order->save();
                
        if($order->getIncrementId()){							
            // Start Automatic Invoice Create								
            if(!$order->canInvoice()){
                Mage::throwException(Mage::helper('core')->__('Cannot create an liveInvoice.'));
            }
             
            $liveInvoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
             
            if (!$liveInvoice->getTotalQty()) {
                Mage::throwException(Mage::helper('core')->__('Cannot save Make an liveInvoice without products.'));
            }
             
            //$liveInvoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
            $liveInvoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
            $liveInvoice->register();
            $saveTransData = Mage::getModel('core/resource_transaction')
            ->addObject($liveInvoice)
            ->addObject($liveInvoice->getOrder());
             
            $saveTransData->save();
}

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Example

I hope you have Got What is php – Create an Invoice after order is placed And how it works.I would Like to have Feed Back From My Blog(Pakainfo.com) readers.Your Valuable Feed Back,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment