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
  • check my answer, if it helps you. Commented Oct 26, 2018 at 22:22

1 Answer 1

2

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' =>  '[email protected]',
    '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.

5
  • 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. Commented Oct 29, 2018 at 5:06
  • @SudhakarSJ, please check my updates answer. for configurable product function addProduct will have extra parameter super_attribute. Commented Oct 29, 2018 at 5:21
  • @SudhakarSJ, I hope my updated answer make sense to you. if not then please let me know. Commented Oct 29, 2018 at 5:42
  • Thank you very much, it's working perfectly. Commented Oct 29, 2018 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. Commented Oct 31, 2018 at 9:48

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.