Creating a pricing table


#1

I would like to create a pricing table. For each product, the table should show 3 prices: the price for 1 item, for 2 items and for 3 items.

There are quantity discounts (they are the same for all products): 10% off when buying 2 items of the same product, 15% off when buying 3 items of the same product.

The price for buying 1 item of a product is what I call the “basePrice” in my front-matter. Example of pricing table:

  • 1x product X: You pay $10 (= basePrice)
  • 2x product X: You pay $18 (2x$10-10%)
  • 3x product X: You pay $22.50 (3x$10-15%)

Also, the pricing table should show how much $ will be saved in total:

  • 1 item: saved $0
  • 2 items: saved $2
  • 3 items: saved $7.50

And finally the per-item-price:

  • 1 item: $10 (= “basePrice”) per item
  • 2 items: $9 per item
  • 3 items: $7.50 per item

How can I get Kirby to generate such a pricing table for me based on the “basePrice”-front-matter?


#2

Don’t know what you want your table to look like, but suppose you want to show one table row per product, with columns for the three prices, then loop through your products, create a new row for each product, and calculate the prices for the based on the base price


<?php $products = page('products')->children()->visible(); ?>
<table>
  <tr>
    <td>Item Price</td>
    <td>Price for 2 items</td>
    <td>Price for 3 items</td>
  </tr>
  <?php foreach($products as $product): ?>
    <?php
    $bp =  $product->basePrice()->float();
    $discount10 = $bp * 0.1;
    $discount15 = $bp * 0.15;
    ?>
    <tr>
      <td><?= $bp ?></td>
      <td><?= ($bp - $discount10) * 2 ?></td>
      <td><?= ($bp - $discount15) * 3 ?></td>
    </tr>
  <?php endforeach ?>
</table>

I assume you can do the other calculations yourself.


#3

Wonderful, thank you.

One more detail: How do I get Kirby to output the (calculated) numbers with 2 decimals, like this:

88.20 (instead of merely 88.2), or 77.00 instead of 77? Is there some number_format-function available? I haven’t found anything about decimals.


#4

Yep: http://us1.php.net/manual/de/function.number-format.php


#5

Thank you!

I have tried to implement this here:

<?php
$number = 1234.56;
setlocale(LC_MONETARY, 'de_CH');
echo money_format('%.2n', $number) . "\n";
?>

But I don’t get it to work. How can I replace the “1234.56” with the calculated numbers like you showed in your reply?


#6

Doesn’t it work like this?

<?= money_format('%.2n', ( $bp - $discount10) * 2) ?>

#7

Yes, that works perfect!

Thank you.