You're viewing the documentation of an older Simple Commerce version. Consider upgrading to the latest for new features and bug fixes.

At the heart of Simple Commerce is the Calculator. The calculator decides the totals of line items, tax total, shipping total etc.

Recalculating the cart

If you need to recalculate the cart yourself, maybe from an addon or some custom code you're writing, you can use the calculateTotals available on an Order.

1Order::find('abc-123')->calculateTotals();

Extending the calculator

There's some situations where you'd want to extend the calculator. Maybe to add your own discounting functionality or hook into some external API for tax etc.

First, you'll need to make your own class, which implements the Calculator interface and likley also extends the base calculator.

1<?php
2 
3namespace App;
4 
5use DoubleThreeDigital\SimpleCommerce\Contracts\Calculator as Contract;
6use DoubleThreeDigital\SimpleCommerce\Orders\Calculator as BaseCalculator;
7 
8class Calculator extends BaseCalculator implements Contract
9{
10 public function calculateLineItem(array $data, array $lineItem): array
11 {
12 // Everything is £5
13 $lineItem['total'] = 500;
14 
15 return [
16 'data' => $data,
17 'lineItem' => $lineItem,
18 ];
19 }
20}

Additionally, you will need to bind your custom class to Laravel's service container, which you can do in a Service Provider.

1// app/Providers/AppServiceProvider.php
2 
3use App\Calculator;
4use Statamic\Statamic;
5 
6...
7 
8public function register()
9{
10 Statamic::repository(
11 \DoubleThreeDigital\SimpleCommerce\Contracts\Calculator::class,
12 Calculator::class
13 );
14}
Last updated 3 months ago Improve this page on GitHub!