Fix: Move carrier live-pricing choice onto ShippingMethod.charge_by
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.
This commit is contained in:
@@ -4,15 +4,17 @@ namespace Modules\Core\Shipping\Carriers\Acs;
|
||||
|
||||
use Lunar\DataTypes\Price;
|
||||
use Lunar\DataTypes\ShippingOption;
|
||||
use Lunar\Facades\Pricing;
|
||||
use Lunar\Shipping\DataTransferObjects\ShippingOptionRequest;
|
||||
use Lunar\Shipping\Interfaces\ShippingRateInterface;
|
||||
use Lunar\Shipping\Models\ShippingRate;
|
||||
use Modules\Core\Shipping\Carriers\Acs\Exceptions\AcsApiException;
|
||||
use Modules\Core\Shipping\Concerns\ResolvesFixedPricing;
|
||||
use Modules\Core\Shipping\Contracts\SupportsLivePricing;
|
||||
|
||||
class AcsRateDriver implements ShippingRateInterface, SupportsLivePricing
|
||||
{
|
||||
use ResolvesFixedPricing;
|
||||
|
||||
public ShippingRate $shippingRate;
|
||||
|
||||
public function __construct(
|
||||
@@ -36,39 +38,19 @@ class AcsRateDriver implements ShippingRateInterface, SupportsLivePricing
|
||||
$shippingMethod = $shippingRate->shippingMethod;
|
||||
$cart = $shippingOptionRequest->cart;
|
||||
|
||||
if (($shippingMethod->data['charge_by'] ?? 'cart_total') !== 'live') {
|
||||
return $this->resolveFixedPrice($shippingRate, $shippingMethod, $cart);
|
||||
}
|
||||
|
||||
$postcode = $cart->shippingAddress?->postcode;
|
||||
|
||||
if (! $postcode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (($shippingRate->pricing_mode ?? 'live') === 'fixed') {
|
||||
return $this->resolveFixedPrice($shippingRate, $shippingMethod, $cart);
|
||||
}
|
||||
|
||||
return $this->resolveLivePrice($shippingRate, $shippingMethod, $cart, $postcode);
|
||||
}
|
||||
|
||||
private function resolveFixedPrice(ShippingRate $shippingRate, $shippingMethod, $cart): ?ShippingOption
|
||||
{
|
||||
$subTotal = $cart->lines->sum('subTotal.value');
|
||||
|
||||
$pricing = Pricing::for($shippingRate)->qty($subTotal)->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(),
|
||||
);
|
||||
}
|
||||
|
||||
private function resolveLivePrice(ShippingRate $shippingRate, $shippingMethod, $cart, string $postcode): ?ShippingOption
|
||||
{
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user