Generic Overview
Bazar comes with flexible tax support by default. You can easily manage tax definitions by using the Bazar\Support\Facades\Tax
facade.
Taxes are stored on the Item
and the Shipping
models. Both implement the Taxable
interface, which enforces to use of a unified method signature for calculating taxes.
Registering Taxes
You may register taxes using the Tax
facade. You can pass a number, a Closure
, or a class (that implements the Bazar\Contracts\Tax
interface) along with the name of the tax.
use Bazar\Support\Facades\Tax;
// Fix tax
Tax::register('fix-20', 20);
// Custom closure tax
use Bazar\Models\Shipping;
use Bazar\Contracts\LineItem;
use Bazar\Support\Facades\Tax;
Tax::register('custom-percent', function (LineItem $model) {
return $model->getPrice() * ($model instanceof Shipping ? 0.3 : 0.27);
});
// Class tax
use Bazar\Contracts\Tax as Contract;
use Bazar\Contracts\LineItem;
use Bazar\Models\Shipping;
use Bazar\Support\Facades\Tax;
class CustomTax implements Contract
{
public function calculate(LineItem $model): float
{
return $model->getPrice() * ($model instanceof Shipping ? 0.3 : 0.27);
}
}
Tax::register('complex-tax', CustomTax::class);
// or
Tax::register('complex-tax', new CustomTax);
Removing Taxes
You may remove registered taxes using the Tax
facade.
use Bazar\Support\Facades\Tax;
Tax::remove('complex-tax');
Disabling Taxes
You may disable tax calculation globally in some scenarios. To do so, call the disable
method on the Tax
facade.
use Bazar\Support\Facades\Tax;
Tax::disable();
Note, when disabling taxes, the previously set taxes won't be updated or recalculated. They stay untouched.