Problem/Motivation

There is no REST layer which supports Cart interaction for a decoupled Cart.

Proposed resolution

Build a new set of REST resources which will allow interaction with the Cart.

Remaining tasks

Proposed endpoints for RESTful Cart, this list is only a start.

/cart/init - POST
Will create a new order and return the uuid to the user for use with subsequent API calls.

/cart/{order uuid} - POST, GET, DELETE, PATCH

POST - Add one or more purchasable entities to the cart.
GET - Full order retrieve, no prices, no taxes. JSON object to represent purchasable enties and their quantities currently in cart.
DELETE - No parameters, clear cart, with parameters remove purchsable entitiy by uuid from cart.
PATCH - Quantity update given uuid ID. An update to 0 quantity will delete the referenced uuid from the cart.

User interface changes

None

API changes

No changes to existing APIs, only new additions

Looking for feedback on the above!

Comments

jgrunert created an issue. See original summary.

mglaman’s picture

Core doesn't support uuid mapping in API calls. JSON API module provided this.

rszrama’s picture

fwiw, I think this proposals misunderstands the REST verbs a little bit. Consider the following basic guidelines:

  • GET should return a collection or an item resource; so a GET on /node should return a collection (or list) of nodes, while a GET on /node/1 should return a single node.
  • POST to a collection resource should create a new item in that resource.
  • DELETE to a collection or an item resource should delete the resource.
  • PATCH should perform a partial update on a resource (and I prefer JSON Patch for the media type on these requests, but I'm willing to go along with core ; ).

For a cart resource, this means a GET to /carts should return all carts the API consumer has access to and /carts/## should return a single cart assuming the consumer has access to it. A POST to /carts should create a new cart order for the API consumer (and I'm fine with having a shortcut like that versus requiring the consumer to know how to create a cart solely through /orders), but to /carts/## would be nonsensical. The best shorthand you could achieve would be a collection resource whose URI includes the cart ID, like /carts/##/order-items. However, in that case, I tend to think we should really just be submitting a POST to the /order-items resource, and the body should include the ID of the cart order this sucker is goin' on.

I documented a lot of this in the Commerce Services project for Commerce 1.x. That project page includes a link to the indispensable e-book on API design from Apigee with the best practices that guides the above. I also posted a complete API user guide to https://www.drupal.org/node/2043077#comment-10105608, which defines how our resources worked and what they are. I'd expect us to do something equivalent inside Commerce 2.x, though we may need to modify it to conform with the Drupal community's idiosyncrasies here and there. Would need to discuss the differences on a case-by-case basis I'd imagine.

jgrunert’s picture

Thank you for the feedback gents. I see where we need to go with the API. I think I was stuck on building more of a BFF layer than a straight API. I'll rework things accordingly.

mglaman’s picture

Status: Needs review » Active

No patch.

Andreas Radloff’s picture

Is anyone working on this?

I'm looking into building a headless Drupal Commerce installation which would be shared between multiple D8 sites.

司南’s picture

So, what is the plan to do this job? commerce_services module seems deprecated, last update is 2014, and there is no any plan of Commerce 2.x

司南’s picture

is there any guideline for developing of ios/android applications in Commerce 2.x

i found the things may help:

  • drupal core module REST, bring CURD operation for all entity, and let us defind REST resource by using Plugin System.
  • drupal core module User, provides some endpoits for login/logout/reset-password/register users in cookies auth
  • the jDrupal contrib module for js lib to use above APIs.
  • ths Services contrib module provides tools for building None-RESTful APIs

no more ways to do.

jgrunert’s picture

I'm going to start working on this again.

司南’s picture

good, need help? I'm in

omarlopesino’s picture

I want help too. @jgrunert, how can we help you?

mglaman’s picture

Assigned: jgrunert » mglaman

Assigning to myself. I had a good discussion at Florida DrupalCamp about ways forward. I've written a draft proposal that I'm having reviewed before posting here.

jasonyarrington’s picture

Ping me about this. We can put some time into it too.

司南’s picture

I am making production project that need this features right now.
I am planing to complete this features in a week.

@mglaman where can see your draft proposal ? can't you share it to me right now ?

bojanz’s picture

bojanz’s picture

Status: Active » Needs review

Changing the status, since we now have a functional Cart API that needs to be either merged into Commerce or made into a contrib project.
We're currently discussing the implementation with the JSON API team, to see whether cooperation is possible on that side.

mglaman’s picture

Please note: if you choose to use that repository, add the following to your composer.json

        "commerce_cart_api": {
            "type": "vcs",
            "url": "https://github.com/mglaman/commerce_cart_api.git"
        },

Be aware it is experimental and will change without notice. Some were bitten by a refactor in a rest.module plugin change (see https://github.com/mglaman/commerce_cart_api/commit/620679f73c085253107d...)

Especially since a goal is to solidify this at DrupalCon.

Sorry for any "silence" on this. Just have not wanted to say "check this out!" then cause breaking changes.

司南’s picture

thank you so much.

司南’s picture

https://github.com/mglaman/commerce_cart_api/pull/10

@mglaman
I found some mistake of the code, I had fixed it, please check my PR.
Im sorry that I forgot to create an new branch.

mglaman’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.