Reverts the earlier per-rate pricing_mode column in favor of extending Lunar's existing charge_by field (cart_total/weight) with a third "live" option, gated by a SupportsLivePricing capability check on the driver. Adds a shared ResolvesFixedPricing trait so any carrier driver can fall back to Lunar's normal price-break resolution, matching the vendor ShipBy driver's own charge_by handling instead of introducing a separate mechanism. Also fixes an incorrect Get() path in the admin form that silently hid the new "live" option.
43 lines
1.5 KiB
PHP
43 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Core\Shipping\Concerns;
|
|
|
|
use Lunar\DataTypes\ShippingOption;
|
|
use Lunar\Facades\Pricing;
|
|
use Lunar\Shipping\Models\ShippingMethod;
|
|
use Lunar\Shipping\Models\ShippingRate;
|
|
|
|
/**
|
|
* Shared by any carrier driver that also supports Lunar's own price-break
|
|
* pricing (charge_by = cart_total | weight) as a fallback to, or standalone
|
|
* alternative for, live API pricing. Mirrors the vendor ShipBy driver's
|
|
* charge_by handling exactly, so behavior is consistent with the rest of
|
|
* Lunar's shipping system rather than inventing a separate mechanism.
|
|
*/
|
|
trait ResolvesFixedPricing
|
|
{
|
|
private function resolveFixedPrice(ShippingRate $shippingRate, ShippingMethod $shippingMethod, $cart): ?ShippingOption
|
|
{
|
|
$chargeBy = $shippingMethod->data['charge_by'] ?? 'cart_total';
|
|
|
|
$tier = $chargeBy === 'weight'
|
|
? $cart->lines->load('purchasable')->sum(fn ($line) => ($line->purchasable->weight_value ?? 0) * $line->quantity)
|
|
: $cart->lines->sum('subTotal.value');
|
|
|
|
$pricing = Pricing::for($shippingRate)->qty($tier)->get();
|
|
|
|
if (! $pricing->matched) {
|
|
return null;
|
|
}
|
|
|
|
return new ShippingOption(
|
|
name: $shippingMethod->name ?: $this->name(),
|
|
description: $shippingMethod->description ?: $this->description(),
|
|
identifier: $shippingRate->getIdentifier(),
|
|
price: $pricing->matched->price,
|
|
taxClass: $shippingRate->getTaxClass(),
|
|
taxReference: $shippingRate->getTaxReference(),
|
|
);
|
|
}
|
|
}
|