This is the documentation for LemonStand V1, which has been discontinued. You can learn more and upgrade your store here.

LemonStand Version 1 Has Been Discontinued

This documentation is for LemonStand Version 1. LemonStand is now offered as a cloud-based eCommerce platform.
You can try the new LemonStand and learn about upgrading here.

How to Display Sale Products

The list_on_sale() method of the Shop_Product class returns a list of products on sale. You can use this method for displaying products on sale on your website. Below we demonstrate different ways of displaying products on sale.

In the Displaying a list of products article we explained how to create a partial (shop:product_list) for displaying a list of products with or without pagination. This partial is included with the demo store which we distribute with the LemonStand installer. You can use this partial for displaying on sale products. Alternatively you can display the list using custom PHP code.

Displaying a list of products on sale with a custom code

The following code outputs a list of all on sale products

<?
  $products = Shop_Product::list_on_sale()->find_all();
  foreach ($products as $product):
?>
  <h3><?= h($product->name) ?></h3>
  <p>
     Original price: <?= format_currency($product->price()) ?><br/>
     Sale price: <?= format_currency($product->get_sale_price(1)) ?><br/>
  </p>
  <p><a href="<?= $product->page_url('/product') ?>">Read more...</a></p>
<? endforeach ?>
{% set products = method('Shop_Product', 'list_on_sale').find_all() %}
{% for product in products %}
  <h3>{{ product.name }}</h3>
  <p>
     Original price: {{ product.price()|currency }}<br/>
     Sale price: {{ product.get_sale_price(1)|currency }}<br/>
  </p>
  <p><a href="{{ product.page_url('/product') }}">Read more...</a></p>
{% endfor %}

Displaying a list of products on sale using the shop:product_list partial

The following code outputs a list of all on sale products using the shop:product_list partial without pagination.

<?
  $this->render_partial('shop:product_list', array(
    'products'=>Shop_Product::list_on_sale()->find_all(),
    'paginate'=>false
  ));
?>
{{ render_partial('shop:product_list', {
    'products': method('Shop_Product', 'list_on_sale').find_all(),
    'paginate': false
  }) }}

Sorting the product list by price or randomly

The list_on_sale() method allows to sort the list of products by name, price or display the product list in random order. By default products are sorted by name. The following code outputs the product list sorted by price in descending order:

<?
  $this->render_partial('shop:product_list', array(
    'products'=>Shop_Product::list_on_sale(array('sorting'=>array('price desc')))->find_all(),
    'paginate'=>false
  ));
?>
{{ render_partial('shop:product_list', {
    'products': method('Shop_Product', 'list_on_sale', {'sorting': ['price desc']}).find_all(),
    'paginate': false
  }) }}

To sort the product list in random order use the rand() function in the sorting parameter:

<?
  $this->render_partial('shop:product_list', array(
    'products'=>Shop_Product::list_on_sale(array('sorting'=>array('rand()')))->find_all(),
    'paginate'=>false
  ));
?>
{{ render_partial('shop:product_list', {
    'products': method('Shop_Product', 'list_on_sale', {'sorting': ['rand()']}).find_all(),
    'paginate': false
  }) }}

Limiting the number of displayed products

You can limit the number of displayed products using the limit() method of the Shop_Product class. Call this method before you call the find_all() method. The following code displays 10 random on sale products.

<?
  $this->render_partial('shop:product_list', array(
    'products'=>Shop_Product::list_on_sale(array('sorting'=>array('rand()')))->limit(10)->find_all(),
    'paginate'=>false
  ));
?>
{{ render_partial('shop:product_list', {
    'products': method('Shop_Product', 'list_on_sale', {'sorting': ['rand()']}).limit(10).find_all(),
    'paginate': false
  }) }}

Next: How to Allow Customers to Provide Item Specific Information
Previous: Tips and Tricks
Return to Tips and Tricks