Merx

Volume-based pricing

Volume-based pricing is a pricing strategy in which the unit price decreases as the purchase quantity increases, incentivizing larger orders by offering tiered discounts tied to volume thresholds.

To realize volume-based pricing you need to make the price of your product dynamic.

Create a custom product page model

If you don’t already have one, create your own product model that extends Merx’ ProductPage class.

First, use the price() method of the parent ProductPage class to get the $price object. Then use this object to update the price using updatePrice() or updatePriceNet().

In the following example, the price is reduced by 5% when a cart line holds 10 or more items of this product.

/site/models/product.php
<?php

use Wagnerwagner\Merx\Price;
use Wagnerwagner\Merx\PricingRule;

class ProductPage extends Wagnerwagner\Merx\ProductPage
{
  public function price(null|string|PricingRule $pricingRule = null, null|float $quantity = null): ?Price
  {
    $price = parent::price($pricingRule, $quantity);
    if ($price !== null && $quantity >= 10) {
      $price->updatePrice($price->price * 0.95);
    }
    return $price;
  }
}

That’s all you need to do — the cart uses this price method and passes the quantity of the line automatically. Without a price for the pricing rule, parent::price() returns null, so check for it before you change the price.

Display reduced price

You can use the same price method to show the reduced price on the product page.

/site/templates/product.php
<?php /** @var ProductPage $page */ ?>
<p class="callout">
If you buy 10 items or more, 
the price per item is reduced to 
<?= $page->price(quantity: 10) ?>.
</p>