Thursday, August 4, 2011

Programmetically Creat Order in Magento

Hello All,

Currently I am working on create order using   API data in magento .
so I write some code for how to create  order programmatic in magento.

this code for simple product and working on product sku.

  $sku="BProduct1";
  $buyInfo = array('qty' => 1);
 // $product[price]=$orderitems->UnitPrice;add this line  if you want to create your order as per custom price
  $quote->addProduct($ product , new Varien_Object($buyInfo));
  $product= Mage::getModel('catalog/product');

 $addressData = array(
                            'firstname' => 'Naresh',
                            'lastname' => 'Tank',
                            'street' => 'Chandkheda',
                            'city' => 'New jersey',
                            'postcode' => '07030',
                            'telephone' => '9995456855',
                            'country_id' => 'US',
                             'region_id' => 'NJ'
                         
                    );
            $billingAddress = $quote->getBillingAddress()->addData($addressData);
           $shippingAddress = $quote->getShippingAddress()->addData($addressData);
         
           $shippingAddress->setCollectShippingRates(true)
                            ->collectShippingRates()
                       ->setShippingMethod('flatrate_flatrate')
                       ->setPaymentMethod('purchaseorder');

  $quote->getPayment()->importData(array('method' => 'purchaseorder',
                                                           'po_number' => 956648787));
       
            $quote->collectTotals()->save();
         
            Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "0");
            $service = Mage::getModel('sales/service_quote', $quote);
            $service->submitAll();
         
            $new_incr_id = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId(Mage::app()->getStore('default')->getId());                              
         
            $order = Mage::getModel("sales/order")->loadByIncrementId(($new_incr_id-1));
                 
            $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
           
            if (!$invoice->getTotalQty()) {
             
                Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
         
            }
           
            $invoice->setRequestedCaptureCase(
            Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE
            );
            $invoice->register();
            $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($invoice)
            ->addObject($invoice->getOrder());
           
            $transactionSave->save();
         
            $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();  

here i consider billing address same as shipping address  u can set different address by passing other array in addData($addressData) method. 
here I use  purchaseorder as payment method and flate rat as shipping methode.




Saturday, July 9, 2011

read html file using url and create pdf

hello all,

this is my first technical blog.
It about read html templet and creat pdf from it using html2pdf creater in php.

 function getPDFContent($url , $pdf_name)

    $pdf=new HTML2FPDF();
    $pdf->AddPage();
     $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,$url);
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,20);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
      if (empty($buffer))
    {
          print "Sorry, Your page not loaded successfully.<p>";
    }
    else
    {
         $pdf->WriteHTML(utf8_decode($buffer));
        $pdf->Output($pdf_name);
    }
}

this function use to create pdf.
you have to pass full path of html file  and file name which you want to name your pdf .
that's it ,now your pdf is present in your current files path.

thanks
naresh