LemonStand Documentation

Displaying products on sale

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

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 to the demo store which we distribute with the LemonStand installer. You can use this partial for displaying discounted products. Alternatively you can display the list using custom PHP code.

Displaying a list of discounted products with a custom code

The following code outputs a list of all discounted products.

<?
  $products = Shop_Product::list_discounted()->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_discounted_price(1)) ?><br/>
  </p>
  <p><a href="<?= $product->page_url('/product') ?>">Read more...</a></p>
<? endforeach ?>

Displaying a list of discounted products using the shop:product_list partial

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

<?
  $this->render_partial('shop:product_list', array(
    'products'=>Shop_Product::list_discounted()->find_all(),
    'paginate'=>false
  ));
?>

Sorting the product list by price or randomly

The list_discounted() 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_discounted(array('sorting'=>array('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_discounted(array('sorting'=>array('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 discounted products.

<?
  $this->render_partial('shop:product_list', array(
    'products'=>Shop_Product::list_discounted(array('sorting'=>array('rand()')))->limit(10)->find_all(),
    'paginate'=>false
  ));
?>

Next: Allowing customers to provide order item specific information
Previous: Tips and Tricks
Return to Tips and Tricks

Comments

No comments posted so far.

Add your comment

Loading form...