duncanmcclean / simple-commerce

A simple, yet powerful e-commerce addon for Statamic.
https://statamic.com/addons/duncanmcclean/simple-commerce
Other
147 stars 41 forks source link

How to handle already sold products which are still in the cart? #842

Closed duncanmcclean closed 1 year ago

duncanmcclean commented 1 year ago

Discussed in https://github.com/duncanmcclean/simple-commerce/discussions/841

Originally posted by **itsmesaskovic** March 3, 2023 We are selling used books via our Simple Commerce setup. Now we have had an issue where a user placed an order, paid for it, but it never got flagged as paid. Looking back in the logs, I found out that the cart was filled with a number of books for the February 10 date. However, on February 10, another visitor also added some of the selected books to the cart and fulfilled the order. Because they were already in our first customers shopping cart, they could place an order with these books on February 22. However, upon confirmation, the order could not be processed automatically, as the books were already out of stock. What would the best practice be to handle this before order submission / payment?

Solution

Simple Commerce should be checking the stock of products in the cart before redirecting the user to off-site gateways.

This won't be an issue if you're using an on-site gateway because the stock is checked before collecting payment.

github-actions[bot] commented 1 year ago

Released as part of v4.5.6.

itsmesaskovic commented 1 year ago

After this release the following happens when testing:

  1. User A adds product X to the basket and sees it in domain.org/shop/cart
  2. User A continues shopping.
  3. User B adds product X to the basket and sees it in domain.org/shop/cart
  4. User B continues to checkout (domaing.org/shop/checkout/information)
  5. User B fulfills the order and pays via Mollie.
  6. User B is redirected (callback) to the success screen.
  7. User A continues to checkout (domaing.org/shop/checkout/information)
  8. User A clicks on submit order
  9. SimpleCommerce checks stock and invalidates the specific product from the cart
  10. User A is redirected to the checkout (information) screen where no product is available
  11. SimpleCommerce (?) redirects the user to an unknown cart screen (/cart instead of /shop/cart?)

image

We have interrupted the default SimpleCommerce process, if I'm correct, since we handle everything on the checkout screen. Is there something additional we need to do to support this new (no stock) flow?

duncanmcclean commented 1 year ago

Simple Commerce should redirect the user back to whatever URL they were on previously (previous to being redirected to the off-site checkout page which should be the /shop/checkout/information page)

Then on that page, if you do {{ session:errors | dump }}, you should see an error which Simple Commerce throws that you can catch and put somewhere for the user to see.

Let me if any of that isn't working as explained and I'll see what I can do to help.

itsmesaskovic commented 1 year ago

That's correct Duncan. The page redirected to is /shop/checkout/information. I just found the issue on our side. We have this code on the information page:

{{ if {sc:cart:count} === 0 }} {{ redirect to="/cart" }} {{ /if }}

Since the cart is emptied the user is automatically redirected to the (wrong) cart. So we should stay on the information page, but with the message that the cart items have change.

Thanks!

duncanmcclean commented 1 year ago

Ah okay, that makes sense!