Question

Hi I am converting a quote to order programatically. 929 is a quote id of a guest user.When i am trying a actual customers quote id it is working fine.but for guest this is not working. giving error as

Fatal error: Call to a member function getIncrementId() on a non-object

here is my code

$quoteObj = Mage::getModel('sales/quote')->load(929); // Mage_Sales_Model_Quote
        $items = $quoteObj->getAllItems();  
         $quoteObj->reserveOrderId();

        //print_r($items);
$addressData = array(
        'firstname' => 'Test',
        'lastname' => 'Test',
        'street' => 'Sample Street 10',
        'city' => 'Somewhere',
        'postcode' => '123456',
        'telephone' => '123456',
        'country_id' => 'US',
        'region_id' => 12, // id from directory_country_region table
);

$billingAddress = $quoteObj->getBillingAddress()->addData($addressData);
$shippingAddress = $quoteObj->getShippingAddress()->addData($addressData);

$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                ->setShippingMethod('flatrate_flatrate')
                ->setPaymentMethod('checkmo');

$quoteObj->getPayment()->importData(array('method' => 'checkmo'));

$quoteObj->collectTotals()->save();

$service = Mage::getModel('sales/service_quote', $quoteObj);
$service->submitAll();
$order = $service->getOrder();

printf("Created order %s\n", $order->getIncrementId());

Please Help.

edit : This code is using rest api with put method.

public function checkout($postParams,$data){
        $response = array();
        $registryId = $postParams['registry_id'];
        $quoteId = $data['quote_id'];
        $quoteObj = Mage::getModel('sales/quote')->load($quoteId);
        $quoteObj->reserveOrderId();
        $BillingData = array(
            'firstname' => $data['first_name'],
            'lastname' => $data['last_name'],
            'street' => $data['street_address'],
            'city' => $data['city'],
            'postcode' => $data['zipcode'],
            'telephone' => $data['phone'],
            'country_id' => $data['country'],
            'region_id' => $data['region'], 
            'quote_id' => $quoteId,
        );
    // addresses

        $billingAddress = $quoteObj->getBillingAddress()->addData($BillingData);
        $shippingAddress = $quoteObj->getShippingAddress()->addData($BillingData);
        $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                ->setShippingMethod('flatrate_flatrate')
                ->setPaymentMethod('checkmo');
        $quoteObj->getPayment()->importData(array('method' => 'checkmo'));
        $quoteObj->collectTotals();
        $service = Mage::getModel('sales/service_quote', $quoteObj);
        $service->submitAll();
        $order = $service->getOrder();
        $IncrementId=$order->getIncrementId();
        $response['success'] = True;
        $response['message'] = "Order Placed Succesfully";
        $response['id']=$IncrementId;
        return $response;
    }

This is returning me the error for getting increent id and no order is placed.This is also adding a quote to quote item table with empty values.

Was it helpful?

Solution

Try following way:

$quoteObj = Mage::getModel('sales/quote')->load(706);
$items = $quoteObj->getAllItems();
$quoteObj->reserveOrderId();

//print_r($items);
$addressData = array(
    'firstname' => 'Test',
    'lastname' => 'Test',
    'street' => 'Sample Street 10',
    'city' => 'Somewhere',
    'postcode' => '123456',
    'telephone' => '123456',
    'country_id' => 'US',
    'region_id' => 12, // id from directory_country_region table
);

// addresses
$quoteShippingAddress = new Mage_Sales_Model_Quote_Address();
$quoteShippingAddress->setData($addressData);
$quoteBillingAddress = new Mage_Sales_Model_Quote_Address();
$quoteBillingAddress->setData($addressData);
$quoteObj->setShippingAddress($quoteShippingAddress);
$quoteObj->setBillingAddress($quoteBillingAddress);

$quoteObj->getShippingAddress()->setShippingMethod('flatrate_flatrate');

$quoteObj->getShippingAddress()->setCollectShippingRates(true);
$quoteObj->getShippingAddress()->collectShippingRates();

$quoteObj->collectTotals();

// set payment method
$quotePaymentObj = $quoteObj->getPayment();
$quotePaymentObj->setMethod('checkmo');
$quoteObj->setPayment($quotePaymentObj);

$quoteObj->collectTotals()->save();

$service = Mage::getModel('sales/service_quote', $quoteObj);
$service->submitAll();
$order = $service->getOrder();

printf("Created order %s\n", $order->getIncrementId());

OTHER TIPS

Your code is working perfectly on my device. I checked for both logged in and guest users and I was successfully able to convert quote into the order on my device. There might be some value missing on your side.

As your code is fine that means there is problem in your objects like quoteObj.

Try to print data present in quoteObj by using getData() function like below -:

echo "<pre>";

print_r($quoteObj->getData());

echo "</pre>";

And check if there is something wrong.

Data in my quote object is as follows. You can compare your data with this data -:

Array

(
    [entity_id] => 7
    [store_id] => 1
    [created_at] => 2017-03-09 14:57:48
    [updated_at] => 2017-03-09 15:05:39
    [converted_at] => 
    [is_active] => 1
    [is_virtual] => 0
    [is_multi_shipping] => 0
    [items_count] => 1
    [items_qty] => 1
    [orig_order_id] => 0
    [store_to_base_rate] => 1
    [store_to_quote_rate] => 1
    [base_currency_code] => USD
    [store_currency_code] => USD
    [quote_currency_code] => USD
    [grand_total] => 105
    [base_grand_total] => 105
    [checkout_method] => 
    [customer_id] => 
    [customer_tax_class_id] => 3
    [customer_group_id] => 0
    [customer_email] => 
    [customer_prefix] => 
    [customer_firstname] => 
    [customer_middlename] => 
    [customer_lastname] => 
    [customer_suffix] => 
    [customer_dob] => 
    [customer_note] => 
    [customer_note_notify] => 1
    [customer_is_guest] => 0
    [remote_ip] => 127.0.0.1
    [applied_rule_ids] => 
    [reserved_order_id] => 100000005
    [password_hash] => 
    [coupon_code] => 
    [global_currency_code] => USD
    [base_to_global_rate] => 1
    [base_to_quote_rate] => 1
    [customer_taxvat] => 
    [customer_gender] => 
    [subtotal] => 100
    [base_subtotal] => 100
    [subtotal_with_discount] => 100
    [base_subtotal_with_discount] => 100
    [is_changed] => 1
    [trigger_recollect] => 0
    [ext_shipping_info] => 
    [gift_message_id] => 
    [is_persistent] => 0
    [virtual_items_qty] => 0
    [taxes_for_items] => Array
        (
        )

    [can_apply_msrp] => 
    [totals_collected_flag] => 1
)

I guess that for Few values, ur order does not created.

Make, Your quote is active and Current Checkout module is guest

$quoteObj->setIsActive(true)
->setCheckoutMethod(Mage_Checkout_Model_Type_Onepage::METHOD_GUEST)
->save()

After adding shipping and billing address .. require to recalculated total by

$quoteObj->collectTotals()->save();

You need to add the checkout method as guest after setting the billing and shipping addresses. Please check the below code, it worked for me hopefully it will help in your case as well.

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

use this code. i use this code in my extension and its works fine

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top