1

I am trying to add items to my cart by using the magento 2 Rest API.

Is it possible to get the Customer token by using the customer session? I have looked into this and tried to implement a mix of a few ways to get the results i require.

public function getCustomerToken(){
  $result = "null";

  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $customerSession = $objectManager->get('Magento\Customer\Model\Session');

  if($customerSession->isLoggedIn()) {
    $customerToken = $this->tokenModelFactory->create();
    $tokenKey = $customerToken->createCustomerToken($customerSession->getCustomer()->getId())->getToken();
    $result = $tokenKey;
  }
  else{
    $result = "null";
  }
  return $result;
}

This is what i have so far. This is currently throwing an error

Undefined property: Vendor\Module\Block\Index\Index::$tokenModelFactory

To add the items to the cart I need 3 things, the product sku, the quantity and the QuoteID.

to get the quote ID, i must create a cart for the user and to do this i need the customers token.

This is the article I have been learning from: http://devdocs.magento.com/guides/v2.1/get-started/order-tutorial/order-create-quote.html

Is it possible to do what i am trying to?

Thanks

  • You don't need to do any of that. Have a look at the answer by @BigDaddyL – André Ferraz Feb 13 '18 at 16:32
2

From my own experiences, I could only seem to get the customer token using this -

http://devdocs.magento.com/guides/v2.1/get-started/order-tutorial/order-create-customer.html#get-token

This passes over the users email and password to get it, which isn't ideal.

I tried using the "/mine" and "/me" endpoint as well, but after Magento 2.1.7 or so, this no longer works.

I have achieved something similar to you though by using the customer email address to get their customer ID, and from this you can add items to their cart using -

/rest/V1/customers/{customerID}/carts

This gives back a cart ID (quote ID) that can be used.

This method uses an admin bearer token in the request, which lets you add items to a customers / guest carts.


Edited in example -

The headers for these requests would look like -

bearertoken

Obviously with the generated admin bearer token.

Do a GET to /rest/V1/customers/search

Use the params to fill in the customer you're trying to finds email address (i'm using Postman here to test) -

enter image description here

Making the endpoint - /rest/V1/customers/search?searchCriteria[filter_groups][1][filters][0][field]=email&searchCriteria[filter_groups][1][filters][0][value]=email@address.com&searchCriteria[filter_groups][1][filters][0][condition_type]=eq

This will return information about the customer, including the customer ID -

{
    "items": [
        {
            "id": 4,
            "group_id": 1,
            "default_billing": "3",
            "created_at": "2017-08-31 15:41:04",
            "updated_at": "2017-08-31 15:41:04",
            "created_in": "Default Store View",
........

You can also filter the response down to only get the email address (http://devdocs.magento.com/guides/v2.1/howdoi/webapi/search-criteria.html) but i'll skip that for now.

Now that you have the customer ID, you can start to populate their cart.

POST to /rest/V1/customers/{customerID}/carts

The response here will be used as the quote ID. For this example I will use "62" as the quote ID.

Now if you do a GET to rest/V1/carts/62

You will be able to see what parts still need filled in (product, shipping, billing, payment etc).

You can pretty much use this guide - http://devdocs.magento.com/guides/v2.1/get-started/order-tutorial/order-add-items.html to complete the order. Instead of using the /carts/mine endpoint, you'd replace /mine with the quote ID from earlier.

If you want me to finish the example fully, let me know.

  • cheers for your response. I am glad i am on the right track with it. – dev Feb 13 '18 at 16:20
  • im unsure of how i would put what you said into practise. could you please give me an example of how to implement it? i know how to get the admin token but unsure how to set the headers and content of the request up. thanks – dev Feb 13 '18 at 16:35
  • I edited in an example that will hopefully help. I didn't add how to get the customers email address though, i'm using a custom integrated service to do this - so giving an example of this part wouldn't help. – BigDaddyL Feb 13 '18 at 17:03

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.