2

I want to load a previous generated quote in magento 2 using REST API. Directly on frontend using custom URL like "/myurl/cart/index" that should redirect to "/checkout/cart" with the products loaded. That I'm doing in API is the following:

1) POST /rest/V1/guest-carts

Return something like: bca5b94b97710599026d73h358e058f7

2) POST rest/V1/guest-carts/bca5b94b97710599026d73h358e058f7/items

{
 "cartId": "bca5b94b97710599026d73h358e058f7",
 "cart_item": {
 "quote_id": "bca5b94b97710599026d73h358e058f7",
 "sku": "mysku",
 "qty": 1
 }
}

the above return the item added. Then:

3) GET /rest/V1/guest-carts/bca5b94b97710599026d73h358e058f7

That return cart object:

{
 "id": 21,
 "created_at": "2018-07-30 18:07:14",
 "updated_at": "2018-07-30 18:32:21",
 "is_active": true,
 "is_virtual": false,
 "items": [
 {
 "item_id": 2,
 "sku": "mysku",
 "qty": 1,
 "name": "Test",
 "price": 150,
 "product_type": "simple",
 "quote_id": "5"
 }
 ]
}

4) Then I've managed to load the quote in my controller with route "/myurl/cart/index" and I can see my quote loaded, something like:

Array
(
 [entity_id] => 21
 [store_id] => 1
 [created_at] => 2018年07月30日 18:07:14
 [updated_at] => 2018年07月30日 18:32:21
 [converted_at] => 
 [is_active] => 1
 [is_virtual] => 0
 [is_multi_shipping] => 0
 [items_count] => 1
 [items_qty] => 1.0000
 [orig_order_id] => 0
 [store_to_base_rate] => 0.0000
 [store_to_quote_rate] => 0.0000
 [base_currency_code] => USD
 [store_currency_code] => USD
 [quote_currency_code] => USD
 [grand_total] => 150.0000
 [base_grand_total] => 150.0000
 [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] => 
 [applied_rule_ids] => 
 [reserved_order_id] => 
 [password_hash] => 
 [coupon_code] => 
 [global_currency_code] => USD
 [base_to_global_rate] => 1.0000
 [base_to_quote_rate] => 1.0000
 [customer_taxvat] => 
 [customer_gender] => 
 [subtotal] => 150.0000
 [base_subtotal] => 150.0000
 [subtotal_with_discount] => 150.0000
 [base_subtotal_with_discount] => 150.0000
 [is_changed] => 1
 [trigger_recollect] => 0
 [ext_shipping_info] => 
 [is_persistent] => 0
 [gift_message_id] => 
)

Which is fine, but now I need to redirect to the cart/checkout in frontend with products loaded to display to the guest user/customer.. Is that possible without create an order?

Thanks,

asked Jul 30, 2018 at 19:56
4
  • are you loading a phtml and block to do this? If yes you should do all the business logic in block and pass output to a function like $block->function(); Also you can redirect in phtml. Commented Jul 30, 2018 at 20:20
  • Hi @VivekKumar, I'm only using controller to load the quote, I wish to redirect to cart or checkout page from there, with quote loaded in order the customer can be able to finish the checkout process by itself. Commented Jul 31, 2018 at 11:35
  • @BrunoSerfe nice post regarding that i can also get the same output but my cart is still empty for guest user any idea regarding this. Commented Mar 19, 2021 at 21:06
  • When you get the Quote in the extension, are the items in it? I try to get them with $this->items = $this->quote->getItems(); but no success. Regards! Commented Jun 26, 2024 at 12:58

1 Answer 1

2

Hi I've managed to resolve my own inquiry with 3 lines of code from my controller, I hope this help someone else:

/* Load quote id */
$quoteId = '21';// this should be loaded from url, I'll make this a TODO
$q = $this->quoteFactory->create()->load($quoteId);
/* Load in checkout session as guest */
$this->checkoutSession->setQuoteId($quoteId);
/* Redirect to cart page */
$this->responseFactory->create()->setRedirect('/checkout/cart/index')->sendResponse();

And that's it, after that I'll be redirected to "checkout/cart/index" from "/myurl/cart/index" with products loaded from that quote after process that code in execute() function.

answered Jul 31, 2018 at 13:22
1
  • I'm glad this question/answer was helpful for other people like you @AdityaShah you're welcome! Commented Aug 6, 2018 at 10:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.