1

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.

1
  • 2
    hey @melvin i post answer. did you try my code?? Commented Mar 14, 2017 at 9:42

5 Answers 5

3

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());
4
  • it is just not working with quotes created through my api
    – Melvin
    Commented Mar 8, 2017 at 8:02
  • ok now i figured out . the orders from my app is having storeid 0 ,when i change it manualy to 1 in db and try my code it works
    – Melvin
    Commented Mar 8, 2017 at 8:15
  • $service = Mage::getModel('sales/service_quote', $quoteObj);echo json_encode($service);exit; is giving me a empty stdClass Object ( )
    – Melvin
    Commented Mar 15, 2017 at 7:18
  • I know it's late but can you put the code for login user
    – Arjun
    Commented Feb 13, 2020 at 11:02
0

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
)
2
  • i am getting only - Array ( [reserved_order_id] => 100000178 ) when store id is 0.when store id is 1 it is giving me the same like your array.
    – Melvin
    Commented Mar 10, 2017 at 7:00
  • $service = Mage::getModel('sales/service_quote', $quoteObj);echo json_encode($service);exit; is giving me a empty stdClass Object ( )
    – Melvin
    Commented Mar 15, 2017 at 7:18
0

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();
1
  • $service = Mage::getModel('sales/service_quote', $quoteObj);echo json_encode($service);exit; is giving me a empty stdClass Object ( )
    – Melvin
    Commented Mar 15, 2017 at 7:18
0

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);
0
$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

4
  • i am not able to get the increment id and and oreder is not getting placed for guest .$quoteObj->getPayment()->importData(array('method' => 'checkmo')); $quoteObj->collectTotals()->save(); $service = Mage::getModel('sales/service_quote', $quoteObj); $service->submitAll(); $order = $service->getOrder(); $IncrementId=$order->getIncrementId(); giving error Call to a member function getIncrementId() on a non-object in
    – Melvin
    Commented Mar 14, 2017 at 12:17
  • load quote by id . and print object data in log. did you find data ?? just check Commented Mar 14, 2017 at 12:20
  • can you check my updated qn
    – Melvin
    Commented Mar 14, 2017 at 13:07
  • $service = Mage::getModel('sales/service_quote', $quoteObj);echo json_encode($service);exit; is giving me a empty stdClass Object ( )
    – Melvin
    Commented Mar 15, 2017 at 7:19

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.