Wednesday, 3 May 2017

programmatically order create in magento

<?php
    error_reporting(0);  
    require_once 'app/Mage.php';
    umask(0);
    Mage::app();  
   
    $row = 1;
    $line_flag = true;
   
    if (($handle = fopen("test.csv", "r")) !== FALSE)
    {
$flag=0;
$arr_row=array();

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
   if($line_flag)
   {
$line_flag = false;
continue;
   }
 
   $current_order_no=$data[1];
 
   $product_name=$data[4];
   $product_sku=$data[5];
   $product_price=$data[9];
 
   $customer_id=$data[12];
 
   $SKU = $product_sku;
   $productid = Mage::getModel('catalog/product')->getIdBySku(trim($SKU));

   $product = Mage::getModel('catalog/product');
   $product ->load($productid);
   $prod=$product->getData();
 
   $product_id = $product->getId();
 
   //if the product is not found in the db code start to add product
 
   if(count($prod)<=5)
   {
//echo "not ext";

$data=array("weight"=>"10","sku"=>$SKU,"name"=>$product_name,"description"=>"description","sdesc"=>"sdesc","price"=>$product_price,"cid"=>"2");

$newproduct = new Mage_Catalog_Model_Product();
$newproduct->setTypeId('simple');
$newproduct->setWeight($data['weight']);
$newproduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$newproduct->setStatus(1);
$newproduct->setSku($data['sku']);
$newproduct->setTaxClassId(0);
$newproduct->setCategoryIds($data['cid']);

$newproduct->setWebsiteIDs(array(1));
$newproduct->setStoreIDs(array(1));
$newproduct->setStockData(array(
'is_in_stock' => 1,
'qty' => 199,
'manage_stock' => 1
));
$newproduct->setAttributeSetId(4);
$newproduct->setName($data['name']);
//$newproduct->setCategoryIds(array(3)); // array of categories it will relate to
$newproduct->setDescription($data['description']);
$newproduct->setShortDescription($data['sdesc']);
$newproduct->setPrice($data['price']);
$newproduct->save();

$product_id = $newproduct->getId();
   }
 
   $customer = Mage::getModel('customer/customer')->load($customer_id); // Customer Id  
   $customer_details=$customer->getData();
     
   $customerAddress = array();
   #loop to create the array
   foreach ($customer->getAddresses() as $address)
   {
      $customerAddress[] = $address->toArray();
   }
   #displaying the array
   //echo '<pre/>';print_r($customerAddress );exit;
 
 
   $firstname=$customer_details['firstname'];
   $lastname=$customer_details['lastname'];
   $company=$customer_details['lastname'];
   $email=$customer_details['email'];
 
   $street=$customerAddress[0]['street'];
   $city=$customerAddress[0]['city'];
   $region_id=$customerAddress[0]['region_id'];
   $region=$customerAddress[0]['region'];
   $postcode=$customerAddress[0]['postcode'];
   $country_id=$customerAddress[0]['country_id'];
   $telephone=$customerAddress[0]['telephone'];
   $fax=$customerAddress[0]['fax'];
 
   $customer_password='';
   $confirm_password='';
 
   $save_in_address_book='0';
   $use_for_shipping='1';
 
 
   $productId=$product_id;
   $store=Mage::app()->getStore();
 
   // Start New Sales Order Quote
   $quote=Mage::getModel('sales/quote')->setStoreId($store->getId());

 
   //Customer Billing Address Data

   $billingAddress = array(
'firstname' => $firstname,
'lastname' => $lastname,
'company' => 'Test',
'email' =>  $email,
'street' => $street,
'city' => $city,
'region_id' => $region_id,
'region' => $region,
'postcode' => $postcode,
'country_id' => $country_id,
'telephone' =>  $telephone,
'fax' => $fax,
'customer_password' => '',
'confirm_password' =>  '',
'save_in_address_book' => '0',
'use_for_shipping' => '1',
   );
 
 
    //Customer Shipping Address Data
       
   $shippingAddress = array(
'firstname' => $firstname,
'lastname' => $lastname,
'street' => $street,
'city' => $city,
'region' => $region,
'region_id' => $region_id,
'postcode' => $postcode,
'country_id' => $country_id,
'telephone' => $telephone,
   );
     
 
   //$quote      = Mage::getModel('sales/quote')->setStoreId($store->getId());
 
   $quote=Mage::getModel('sales/quote')->setStoreId(1);
 
 
   
   //Load Product and add to cart
   $product_cart=Mage::getModel('catalog/product')->load($productId);  
   $buyInfo=array('qty' => 1);  
   $quote->addProduct($product_cart, new Varien_Object($buyInfo));


   // Add Billing Address
   $quote->getBillingAddress()
   ->addData($billingAddress);

 
   //Add Shipping Address and set shipping method  
   $quote->getShippingAddress()
   ->addData($shippingAddress)      
   ->setCollectShippingRates(true)
   ->setShippingMethod('freeshipping_freeshipping')
   ->setCouponCode('test')
   ->collectTotals();
       $quote->setCouponCode('test');
   $quote->collectTotals()->save();

   /*

   //Set Customer group As Guest    
   $quote->setCheckoutMethod('guest')
->setCustomerId(null)
->setCustomerEmail($quote->getBillingAddress()->getEmail())
->setCustomerIsGuest(true)
->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
*/
 
$quote->setCustomerId($customer_id)
        ->setCustomerEmail($email)
        ->setCustomerIsGuest(false)
        ->setCustomerGroupId(1);


   //Set Payment method
 
   $quote->getPayment()->importData(array('method' => 'cashondelivery'));
   $quote->collectTotals()->save();


   //Save Order With All details




   $service = Mage::getModel('sales/service_quote', $quote);
   $service->submitAll();
   $increment_id = $service->getOrder()->getIncrementId();
 
 
 
   Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "1");
 
   //Send Order Mail
 
   $order_mail = new Mage_Sales_Model_Order();
   $order_mail->loadByIncrementId($increment_id);
   $order_mail->sendNewOrderEmail();
 
   //echo $increment_id;

   /*  For Invoice start */


   $order=Mage::getModel("sales/order")->loadByIncrementId($increment_id);
   //Mage::getModel("sales/order")->loadByIncrementId($incrementId);


   $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
   $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
   $invoice->register();
   $transaction = Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
   ->addObject($invoice->getOrder());
   $transaction->save();
   if($order->canShip()):
$itemQty =  $order->getItemsCollection()->count();
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$shipment = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $shipment->create($increment_id);
endif;
   $order->setData('state',"complete");
   $order->setStatus("complete");
 
   //$history = $order->addStatusHistoryComment('Order marked as complete automatically.',false);
 
   $history = $order->addStatusHistoryComment('Order Number: '.$current_order_no);
 
 
   $history->setIsCustomerNotified(false);
   $order->save();
   $id=$order->getId();


/* --------  End --------------------------------------------------- */
 
 
}

fclose($handle);

    }
    else
    {
echo "no file found";
    }
?>

No comments:

Post a Comment