Search code examples
phpmysqlpaypalstore

Payments, shopping basket and add item to basket only for half hour


Here is a site, which after clicking "Dodaję" (means "add" in polish language) item goes to basket for 30 minutes: https://remix.pl/

While the item is in a basket, the item can not be purchased by others. I checked their basked: when I removed cookies, items were removed from basket (so the basket uses php session) and items were again available to buy. I would like to know how did they do that (does their basket use database to store date time of added item to count 30 minutes or the script can be done another way?). Maybe it is a Cron Job or something? What is the best way to do it via php or it is impossible to do something like that with php? I do not mean displaying time ticking/passing, because it need some jQuery, js etc. but just the essence of the script.

I ask about it, because I made an online store for the client, who sells used clothes like that remix.pl so the item quantity is always 1. I have to make some blockade for products, which are added to basket by one user to add the same item for another one user. I do not have mind how to do it.

And here is another one question: Is it possible to remove item quantity only then, when the item is paid via paypal for example? Chemistry for example: Item quantity: 99 After adding to basket: still 99 After placing an order to MySQL database: still 99 After paid by paypal for example: 98 Is it a good practice and is it possible to run a script after the item is paid? Thanks a lot!


Solution

  • Create a basket table in your database as well. When sy places an item in his basket, insert a record into the basket table as well, along with a timestamp of the entry. When you query the products table, you consider those products unavailable, which are in the basket table and the timestamp value is less than 30 mins old. You can ignore any reservations in the basket, which are older than 30 minutes. You can clean up the basket table from either a scheduled script or from any regularly run page's code.

    Regarding your 2nd question: of course you only deduct the sold product quantity from stock when the payment is successfully completed. Paypal api provides callback for that, see instant payment notifications