Forum

Thread tagged as: Question, Shop

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

Stephen Turvey

Stephen Turvey 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Stephen,

You can get the items with perch_shop_order():

if (perch_member_logged_in() && perch_shop_order_successful()) {
    $orderID = perch_shop_successful_order_id();

    $order = perch_shop_order($orderID, [
        'skip-template' => true,
    ]);

    // find items from $order
}

This function is triggered when the browser is redirecting from the successful stripe payment to the return_url success page

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 to true 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.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

You can loop through the items with foreach to get each item's data:

// ordered items
$items = $order[0]['items'];

// loop through the items
foreach($items as $item){
    $sku = $item['sku'];
    $productID = $item['productID'];

    // do something with the 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:

if sku 001 exists { // do this in db }
if sku 002 exists { // do this in db }
etc

Any help in that respect greatly appreciated.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

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:

$product_skus = array();

// loop through the items
foreach($items as $item){
$product_skus[] = $item['sku'];
}

// output the content of the array
print '<pre>';
print_r($product_skus);
print '</pre>';

// do something with the data
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

How can I get all items together separated by a comma?

Just the SKUs separated by comma?

Yes please.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

You can use PHP's implode() function:

$product_skus = array();

// loop through the items
foreach($items as $item){
$product_skus[] = $item['sku'];
}

// join the array elements with a comma
$comma_separated = implode(",", $product_skus);

Great, thanks for your help Hussein.