Skip to content

Allow shipping tax rate to be determined by a callable#2423

Merged
alecritson merged 8 commits intolunarphp:1.xfrom
ryanmitchell:feature/shipping-tables-custom-tax
Mar 30, 2026
Merged

Allow shipping tax rate to be determined by a callable#2423
alecritson merged 8 commits intolunarphp:1.xfrom
ryanmitchell:feature/shipping-tables-custom-tax

Conversation

@ryanmitchell
Copy link
Copy Markdown
Contributor

Adds support for a callable config value for shipping_rate_tax_calculation, allowing applications to provide custom logic to determine which tax class should be applied to shipping rates.

Previously the config only accepted 'default' (system default tax class) or 'highest' (highest tax rate among cart lines). Any value other than 'default' would fall through to the highest-rate resolver, with no way to supply arbitrary logic.

Changes

  • ShippingRate::getShippingOption() — when shipping_rate_tax_calculation is set to a callable (closure or invokable class), it is called with the Cart instance and the return value is used as the resolved TaxClass

Usage

// config/shipping-tables.php
'shipping_rate_tax_calculation' => function (\Lunar\Models\Cart $cart): \Lunar\Models\TaxClass {
    // custom logic — e.g. return a fixed tax class for B2B customers
    return $cart->user?->isBusinessCustomer()
        ? TaxClass::where('name', 'B2B')->first()
        : TaxClass::getDefault();
},

An invokable class is also supported:

'shipping_rate_tax_calculation' => [\App\Shipping\CustomTaxClassResolver, 'handle'],

@alecritson
Copy link
Copy Markdown
Collaborator

Thanks @ryanmitchell I think we should avoid closures in config file as this will stop serialization, also maybe instead (or on top) of having to provide a handle [MyTaxRateCalculator::class, 'calculate'] we make use of invokable classes?

Finally I think if we can simply or tidy up the nested if statements that would be ideal.

@ryanmitchell
Copy link
Copy Markdown
Contributor Author

@alecritson nested ifs are removed.

It already supports invokeable classes (eg 'shipping_rate_tax_calculation' => MyInvokableClass::class).

@alecritson alecritson self-requested a review March 30, 2026 14:13
@alecritson alecritson merged commit a2dce80 into lunarphp:1.x Mar 30, 2026
44 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Roadmap Mar 30, 2026
@ryanmitchell ryanmitchell deleted the feature/shipping-tables-custom-tax branch March 31, 2026 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants