0

I am trying to store multiple products to cart and getting cart id of the products list for generating order in Magento 1.9. I don't know how to create cart and order for guest checkout.

1 Answer 1

0

Can you try with below code used for pragmatically create order in magento.

$productids=array(1,2,4,3);
 $website = Mage::app()->getWebsite()->getId();
 $store = Mage::app()->getStore();
 // Start New Sales Order Quote
 $quote = Mage::getModel('sales/quote')->setStoreId($store->getId());

 // Set Sales Order Quote Currency
 $quote->setCurrency(Mage::app()->getStore()->getBaseCurrencyCode());

$customer = Mage::getModel('customer/customer');

$customer->setWebsiteId($website->getId())
         ->setStore($store)
         ->setFirstname($firstName)
         ->setLastname($lastName)
         ->setEmail($email);  


 // Assign Customer To Sales Order Quote
 $quote->assignCustomer($customer);

     // Configure Notification
 $quote->setSendCconfirmation(1);
 foreach($productids as $id){
     $product=Mage::getModel('catalog/product')->load($id);
     $quote->addProduct($product,new Varien_Object(
 array(
   'qty'   => 1,
   'super_attribute' => [$sizeAttribute->getId() => $valueMedium, 
                 $colorAttribute->getId() => $valueGreen ]
   )
 ));
 }     
 // Set Sales Order Billing Address
 $billingAddress = $quote->getBillingAddress()->addData(array(
     'customer_address_id' => '',
     'prefix' => '',
     'firstname' => 'john',
     'middlename' => '',
     'lastname' =>'Deo',
     'suffix' => '',
     'company' =>'', 
     'street' => array(
             '0' => 'Noida',
             '1' => 'Sector 64'
         ),
     'city' => 'Noida',
     'country_id' => 'IN',
     'region' => 'UP',
     'postcode' => '201301',
     'telephone' => '78676789',
     'fax' => 'gghlhu',
     'vat_id' => '',
     'save_in_address_book' => 1
 ));

 // Set Sales Order Shipping Address
$shippingAddress = $quote->getShippingAddress()->addData(array(
     'customer_address_id' => '',
     'prefix' => '',
     'firstname' => 'john',
     'middlename' => '',
     'lastname' =>'Deo',
     'suffix' => '',
     'company' =>'', 
     'street' => array(
             '0' => 'Noida',
             '1' => 'Sector 64'
         ),
     'city' => 'Noida',
     'country_id' => 'IN',
     'region' => 'UP',
     'postcode' => '201301',
     'telephone' => '78676789',
     'fax' => 'gghlhu',
     'vat_id' => '',
     'save_in_address_book' => 1
 ));
 if($shipprice==0){
     $shipmethod='freeshipping_freeshipping';
 }

 // Collect Rates and Set Shipping & Payment Method
 $shippingAddress->setCollectShippingRates(true)
                 ->collectShippingRates()
                 ->setShippingMethod('flatrate_flatrate')
                 ->setPaymentMethod('checkmo');

 // Set Sales Order Payment
 $quote->getPayment()->importData(array('method' => 'checkmo'));

 // 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();

 // Resource Clean-Up
 $quote = $customer = $service = null;

 // Finished
 echo $increment_id;
5
  • In this code, you are passing products ids only, I want to store products with options(colour, size). It is possible to add products to cart with attribute options and get the cart id or quote id for processing a guest checkout. Here you are creating customer also. But for guest checkout, there is no need to create a customer account. Oct 26, 2018 at 7:03
  • I have updated my code please check. Oct 26, 2018 at 8:04
  • Thanks, I need one more clarification, how to create an order for configurable products. In your case super_attribute colour and size how to get the exact colour and size what we selected while adding the product to cart. I have a little confusion of configurable products order. Oct 26, 2018 at 10:38
  • This link will may help you..magento.stackexchange.com/questions/104470/… Oct 26, 2018 at 12:01
  • I need one clarification inm custom block, Please magento.stackexchange.com/questions/251916/… Check this link. Nov 30, 2018 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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