Creating a pricing table

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?

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(); ?>
    <td>Item Price</td>
    <td>Price for 2 items</td>
    <td>Price for 3 items</td>
  <?php foreach($products as $product): ?>
    $bp =  $product->basePrice()->float();
    $discount10 = $bp * 0.1;
    $discount15 = $bp * 0.15;
      <td><?= $bp ?></td>
      <td><?= ($bp - $discount10) * 2 ?></td>
      <td><?= ($bp - $discount15) * 3 ?></td>
  <?php endforeach ?>

I assume you can do the other calculations yourself.

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.


Thank you!

I have tried to implement this here:

$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?

Doesn’t it work like this?

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

Yes, that works perfect!

Thank you.

With this solution, it works on my localhost, I get “Fr.” for Swiss Franks. But on my remote server (linode), I get $ for USD.

I tried this after searching Google for a solution:

    $bp =  $page->basePrice()->float();
    $discount05 = $bp * 0.05;
    $discount10 = $bp * 0.1;
    $discount15 = $bp * 0.15;
    setlocale (LC_ALL, 'de_CH');
    setlocale(LC_MONETARY, 'de_CH');

In other words, I added this:

setlocale (LC_ALL, 'de_CH');

For output I have this in my template, as an example:

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

Like I said: The math is all correct, but the remote server still outputs $, and not “Fr.” Is there something wrong with money_format?

Someone recommended me this: numberformatter.formatcurrency

Is that the solution? How would I implement this into my template?

The locale may not be correct on your remote host, please check what is installed on your server with

locale -a

(i.e. you might have change it to de_CH.UTF-8, for example, depending on what the above tells you)