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,

  • 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. – Vivek Kumar Jul 30 '18 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. – Bruno Serfe Jul 31 '18 at 11:35
1

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.

  • I'm glad this question/answer was helpful for other people like you @AdityaShah you're welcome! – Bruno Serfe Aug 6 '18 at 10:46

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.