Lemonstand get unpaid orders by customer

December 13, 2012    lemonstand lemonstand orders unpaid orders

During the checkout process Lemonstand automatically clears your shopping cart in favour of creating a Shop_Order object which is pretty static and can be paid for at any point in the future. This object represents the state of your cart when you clicked pay now.

Sometimes (as many of you will know) a user will click pay and not actually either a) want to pay or b) able to pay successfully. Unfortunately in most theme designs the cart is usually fairly prominent in the UI design and serves as a reference point before you checkout.

This is fine, that is, until a user clicks pay. After they click pay if they choose to navigate away little do they know but they'll find it tricky to get back to that point again without visiting a 'My account' page or something similar. Which at this early stage and since in alot of users eyes your not a customer with an account until you check out and complete an order is not great.

We wanted to be able to show the customer there unfinished order in two places so to make it a little easier to pick up where you left off. Below I discuss a couple of ways that we have implemented this.

*Note: Lemonstand do not provide a function for selecting unpaid orders.

/* The order status id way */
$unPaid = Shop_Order::create()->where('customer_id = ?',$this->customer->id)->where('status_id != ?',$your_unpaid_status_id)->limit(2)->find_all(); ?>
/**
  * The more flexible way perhaps for sellable themes.
  * - works best when no status id is known */
$unPaid = Shop_Order::create()->where('customer_id = ?',$this->customer->id)->find_all();
$un_paid = 0;
if(count($unPaid) > 0)
{
    foreach($unPaid as $u_order)
    {
        if(!$u_order->is_paid())
        {
            $un_paid++;
            # Do what you like here.
            echo 'Order ID '.$u_order->id.' is unpaid';
        }
    }
}
/**
  * The tidy way for a non re-sellable theme.
  * - loaded in through a custom module (perhaps containing helper functions)
  * - works best when no status id is known */

# To be placed in your custom module.
function has_unpaid_orders($customer_id){
    $orderCount = 0;
    if($customer_id)
    {
        $unPaid = Shop_Order::create()->where('customer_id = ?',$customer_id)->find_all();
        if(count($unPaid) > 0)
        {
            foreach($unPaid as $u_order)
            {
                if(!$u_order->is_paid())
                {
                    $orderCount++;
                }
            }
        }
    }
    return $orderCount;
}
/* Called from your template like this */
Your_Helper::has_unpaid_orders($this->customer->id);

/* or */
if(Your_Helper::has_unpaid_orders($this->customer->id))
{
    # do something
}

If you are unsure what we mean by a custom helper module. Shortly I will describe in another post how you can achieve this.


blog comments powered by Disqus