• Publisert
  • 1 min

Finding shopping cart by ID in Commerce

Once in a while, you might just need to retrieve shopping carts outside a given user context. Since it's not super intuitive in the Commerce API's, here's how.

To me, "Get item by ID" should be the most basic routine one can provide in any form of data API. Sadly, this is not the case in the Commerce API's. However, even though the standard API's don't give you an easy way of retrieving carts by their ID(s), it is in fact possible.

An example that comes to mind is when doing server-to-server-callbacks from payment providers. The built in cart API does not seem to support this, and all the methods I can find that loads a specific cart for you requires things like the metadata context or the customer ID. Which I happen to not have in this context. You can probably provide the customer ID to your payment provider, and make it send it back along with the callback parameters - but to me that's a bit of a clumsy solution.

A cart has an OrderGroupId, which is a neat reference in your payment provider. At least that's what I'm using now as the order ID in the payment provider. When the ID comes back in some callback function, you can do the following to get back the relevant shopping cart to run it through your workflows.

I do know this is extremely fragile and horribly nasty. At the moment, however, this seems like the only way of doing this. I've been all around decompiled binaries and checked out every corner of the publicly available API's, and couldn't find anything better.

I wish this blog post is wrong and that there is a better way. If so, inform me please. If not, I hope it's utterly obsolete in a few weeks.

And also, here's the feature request for a better solution.