Archive for the ‘Lemonstand’ Category

Lemonstand get unpaid orders by customer

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.

Lemonstand back to search or category results

Today I had to add a basic back to search results implementation and thought i’d blog it for future reference.

I could not find anything implemented in the documentation so decided to add this snippet of code to any of the category or search pages that I had. Be sure to add it inside of the partial which gets re-loaded during pagination changes to continuously update the session as the user browses.

<?php Phpr::$session->set('back_r', $_SERVER['REQUEST_URI']); ?>

You can imagine whats next, open your product page / product partial (depending on your setup) and add the following lines where you would like to display the back to search results link.

$back_r = Phpr::$session->get('back_r');
if($back_r)
{
    echo '<a href="'.root_url($back_r).'" title="Back to search results">&laquo; back</a>';
}

Hopefully this might be of use to someone else.

Lemonstand simple display product list from category

I quite often get asked how to achieve this easily by friends and colleagues and in there defence it’s not ‘clearly’ documented. So I thought i’d knock something up really quickly to show you how it can be done.

In this example I create a category called ‘Featured’ and I importantly set the ‘API code’ for this category to ‘featured’. This is found at the very bottom of the category creation / edit page.

I want to pull in some featured products into my homepage so I dive into the CMS and pull up the homepage.

You’ll want to enter the below snippet into the template.

<?
$category = Shop_Category::create()->find_by_code('featured');
$this->render_partial('shop:product_list', array(
    'products'=>$category->list_products()->find_all()
)) ?>

This snippet first fetches the correct category object and then renders a partial called shop:product_list (or shop;product_list.htm if you use template based files) with all of the products under this category.

Now you just need to make your partial and add some basic code to it.

I’m not going to dive into the full extent of what you can now do with the below code but just get you on your way.

In your shop:product_list partial add the following code:

<ul>
<? foreach ($products as $product): ?>
<li>
<h3><a href="<?=  $product->page_url('/product') ?>"><?= h($product->name) ?></a></h3>
<p><?= h($product->short_description) ?></p>
</li>
<? endforeach ?>
</ul>

And that’s all there is to it. You’ll now be getting a list of all the products in this category on your homepage. Of course it is also worth noting you could use this same partial in many places across your site. It’s use will probably largely depend on how heavily uniform or customised the section on your site is.

Hope this is of some use.