Forum
Check which products have been ordered
When customers make a purchase on my site I have a function which updates a separate database.
Before this was easy because I only had one product, so the logic was something like:
if perch_shop_order_successful() {
$upgrade_status = 1;
}
But now I have multiple products so I want the logic to be:
if perch_shop_order_successful() {
// if product 001 was purchased
$upgrade_status = 1;
// elseif product 002 was purchased
$upgrade_status = 2;
// elseif product 003 was purchased
$upgrade_status = 3;
}
This function is triggered when the browser is redirecting from the successful stripe payment to the return_url success page. How do I check which products were purchased so I can update the db with the required values?
Thank you
Hello Stephen,
You can get the items with
perch_shop_order()
:You could build an app that listens to the event the Shop app fires on successful order instead.
The customer can technically visit the page multiple times and
perch_shop_order_successful()
will evaluate totrue
within the same session. It may be unlikely, but it is not ideal that a simple page refresh or perhaps re-visiting the page from browser history can trigger your function (which may add duplicate records to your other database).This is a good use case to use the Perch API.
Hi Hussein,
How would I get the skus from that $order array?
Thanks for the other info too.
You can loop through the items with
foreach
to get each item's data:If I echo $sku or $productID that gives me the last item in the loop. How can I get all items together separated by a comma?
"001, 002, 003"
Alternatively, I'd like to be able to do a "check if exists" so I can run the database updates like:
Any help in that respect greatly appreciated.
I don't know how your function works so I can't suggest the best way to do things. That's why I added the comment
// do something with the data
.For example, you can add it to an array:
Just the SKUs separated by comma?
Yes please.
You can use PHP's
implode()
function:Great, thanks for your help Hussein.