isAvailable()) { return false; } $cart = Cart::getCart(); if (! $cart) { return false; } $shippingAddress = $cart->shipping_address; if (! $shippingAddress) { return false; } $countryCode = $shippingAddress->country; // 检查该国家是否在允许列表中 $allowedCountries = $this->getConfigData('allowed_countries'); if ($allowedCountries) { $allowedList = array_filter(explode(',', $allowedCountries)); if (! empty($allowedList) && ! in_array($countryCode, $allowedList)) { return false; } } $stateCode = $shippingAddress->state; // 查询匹配的费率:先精确匹配 country+state,再匹配 country+* $rate = ShippingZoneRate::active() ->byCountry($countryCode) ->byState($stateCode) ->byPrice($cart->base_grand_total) ->byRaw($countryCode, $stateCode) ->first(); if (! $rate) { return false; } $cartShippingRate = new CartShippingRate; $cartShippingRate->carrier = $this->getCode(); $cartShippingRate->carrier_title = $rate->carrier_title; $cartShippingRate->method = $this->getMethod(); $cartShippingRate->method_title = $rate->delivery_type; $cartShippingRate->method_description = $rate->delivery_description; $cartShippingRate->price = core()->convertPrice($rate->price); $cartShippingRate->base_price = $rate->price; return $cartShippingRate; } /** * Get country options for the multiselect field in admin configuration. */ public function getCountryOptions(): array { return core()->countries()->map(function ($country) { return [ 'title' => $country->name, 'value' => $country->code, ]; })->toArray(); } }