Forum

Thread tagged as: Question, Shop

Perch Shop Orders

Hi,

Is there any way to check if perch shop orders exist?

On our profile page we want to show the customers orders, but if none exist just display a message like "You have no orders yet".

I figure we could use some conditional statement like:


if perch_orders() { perch_shop_orders() else { echo "You have no orders yet"; }

We know the above won't work, but wondered if there was a function to test if there are any orders to return, or check if the order count is above zero?

Thanks, Alex

Alex Bennett

Alex Bennett 0 points

  • 3 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

As is possible with many other Perch functions, you would have to use the perch_shop_orders() option skip-template, save the output in a variable, inspect that variable for any returned data and then run your if statement.

perch_shop_orders() will be empty if there are no orders, therefore you can just

$orders = perch_shop_orders([// empty], true);
 if ($orders) {
    echo $orders;
}
else {
    echo 'no orders';
}

Thank you Robert,

That worked great!

In case it's useful to anyone, this is my final code on the profile (php) page:

$orders = perch_shop_orders([], true);

    if (perch_member_logged_in()) {

        if ($orders) {
            echo $orders;
        } else {
            echo '<h1>You currently have no orders</h1>';
        }

        }else{
            echo '<a href="/members/">Please log in</a>';
            perch_members_login_form();
        }