Fix: Resolving box now by instance, not by type. Also, fixing bug where missing shipping driver would break checkout
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Shipping\Support;
|
||||
|
||||
use Lunar\Shipping\Facades\Shipping;
|
||||
use Lunar\Shipping\Models\ShippingMethod;
|
||||
use Modules\Core\Shipping\Carriers\BoxNow\BoxNowRateDriver;
|
||||
|
||||
/**
|
||||
* Whether something is Box Now — a checkout option, a shipping method, a
|
||||
* driver key — by checking that the driver is a BoxNowRateDriver, never by
|
||||
* comparing names: an option's identifier is the merchant-typed method
|
||||
* code (may well not be "box-now"), and the driver may be registered under
|
||||
* any key.
|
||||
*/
|
||||
class BoxNowShipping
|
||||
{
|
||||
public static function isOption(?string $identifier): bool
|
||||
{
|
||||
return $identifier !== null
|
||||
&& self::isMethod(ShippingMethod::where('code', $identifier)->first());
|
||||
}
|
||||
|
||||
public static function isMethod(?ShippingMethod $method): bool
|
||||
{
|
||||
return self::isDriver($method?->driver);
|
||||
}
|
||||
|
||||
/**
|
||||
* A shipping driver key (a method's `driver`, a shipment's `carrier`)
|
||||
* that resolves to Box Now. getSupportedDrivers() rather than
|
||||
* Shipping::driver(), which throws for a key no longer registered.
|
||||
*/
|
||||
public static function isDriver(?string $driver): bool
|
||||
{
|
||||
return $driver !== null
|
||||
&& Shipping::getSupportedDrivers()->get($driver) instanceof BoxNowRateDriver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Codes of every Box Now shipping method — for marking options in bulk.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public static function codes(): array
|
||||
{
|
||||
return ShippingMethod::all()
|
||||
->filter(fn (ShippingMethod $method) => self::isMethod($method))
|
||||
->pluck('code')
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user