0

I am trying to create an order for guest checkout programmatically. I have gone through a lot of articles. Finally, I got one stackExchange link for creating order. But In that code, they are not showing how to get cart product list and assign to guest checkout.

This is code for reference.

$checkout = Mage::getSingleton('checkout/type_onepage');
$checkout->initCheckout();


$checkout->saveCheckoutMethod('guest');


$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$emailaddress = $_POST["email"];
$confirmemailaddress = $_POST["cemail"];

if(isset($_POST['tel'])):
$telephone = $_POST['tel'];
else:
$telephone = "Not Applicable";
endif;

if(isset($_POST['postcode'])):
$postcode = $_POST['postcode'];
else:
$postcode = "Not Applicable";
endif;

if(isset($_POST['addr1'])):
$street1 = $_POST['addr1'];
else:
$street1 = "Not Applicable";
endif;

if(isset($_POST['addr2'])):
$street2 = $_POST['addr2'];
else:
$street2 = "Not Applicable";
endif;

if(isset($_POST['city'])):
$city = $_POST['city'];
else:
$city = "Not Applicable";
endif;

$billingAddress =  array (
'firstname' => $firstname,
'lastname' => $lastname,
'email' => $emailaddress,
'street' => Array(
    '0' => $street1,
    '1' => $street2
),
'city' => $city,
'postcode' => $postcode,
'country_id' => 'GB',
'telephone' => $telephone,
);

$shippingAddress =  array (
'firstname' => $firstname,
'lastname' => $lastname,
'email' => $emailaddress,
'street' => Array(
    '0' => $street1,
    '1' => $street2
),
'city' => $city,
'postcode' => $postcode,
'country_id' => 'GB',
'telephone' => $telephone,
);

$checkout->saveBilling($billingAddress,null);

$checkout->saveShipping($shippingAddress,null);

$checkout->getQuote()->getShippingAddress()->setShippingMethod('freeshipping_freeshipping')->collectTotals();

$checkout->savePayment(array('method'=>'free'));

$checkout->saveOrder();

foreach( Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item ){
Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save();
}

Please help to get code Asap. Thanks.

1

try below code --

$quote = Mage::getModel('sales/quote')
        ->setStoreId(Mage::app()->getStore('default')->getId());

$product = Mage::getModel('catalog/product')->load(1); /* your product ID */
$buyInfo = array('qty' => 1); // for simple product

// for configurable product
/* $buyInfo = array('qty' => 1,
                    'super_attribute' =>array(131=>53,142=>50)
   );

     // here 131 color attribute id and 53 is color option(RED) id, 
      142 is size attribute id and 50 is size option(XL) id  
*/
    $quote->addProduct($product, new Varien_Object($buyInfo));

// adding billing address
$billingAddress = array(
    'firstname' => 'Mohan',
    'lastname' => 'Raj',
    'company' => 'Webstocks',
    'email' =>  'test@test.com',
    'street' => array(
        'Sample Street Line_1',
        'Sample Street Line_2'
    ),
    'city' => 'City',
    'region_id' => '',
    'region' => 'State/Province',
    'postcode' => '12345',
    'country_id' => 'NL',
    'telephone' =>  '1234567890',
    'fax' => '123456987',
    'customer_password' => '',
    'confirm_password' =>  '',
    'save_in_address_book' => '0',
    'use_for_shipping' => '1',
);
$quote->getBillingAddress()
        ->addData($billingAddress);

//set shippingaddress, shipping method, payment method
$quote->getShippingAddress()
        ->addData($billingAddress)
        ->setShippingMethod('flatrate_flatrate')
        ->setPaymentMethod('cashondelivery')
        ->setCollectShippingRates(true)
        ->collectTotals();

$quote->setCheckoutMethod('guest')
            ->setCustomerId(null)
            ->setCustomerEmail($quote->getBillingAddress()->getEmail())
            ->setCustomerIsGuest(true)
            ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
$quote->getPayment()->importData( array('method' => 'cashondelivery'));
$quote->save();
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();

above code is tested in an external file.

  • Thanks, I have one more clarification, you are adding products based on product id, This is for the simple product. If we have multiple products with configurable products, That time how to pass the products ids to Quote. – Sudhakar SJ Oct 29 '18 at 5:06
  • @SudhakarSJ, please check my updates answer. for configurable product function addProduct will have extra parameter super_attribute. – Shashank Kumrawat Oct 29 '18 at 5:21
  • @SudhakarSJ, I hope my updated answer make sense to you. if not then please let me know. – Shashank Kumrawat Oct 29 '18 at 5:42
  • Thank you very much, it's working perfectly. – Sudhakar SJ Oct 29 '18 at 6:00
  • Can you please check this link [magento.stackexchange.com/questions/248410/…. I am getting an error while updating the quantity in the quote. – Sudhakar SJ Oct 31 '18 at 9:48

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.