fluent-cart
/
app
/
Modules
/
Shipping
/
Http
/
Controllers
/
Frontend
/
ShippingFrontendController.php
ShippingFrontendController.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Modules/Shipping/Http/Controllers/Frontend/ShippingFrontendController.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCart\App\Modules\Shipping\Http\Controllers\Frontend; |
| 4 | |
| 5 | use FluentCart\App\Http\Controllers\Controller; |
| 6 | use FluentCart\App\Models\ShippingMethod; |
| 7 | use FluentCart\App\Services\Localization\LocalizationManager; |
| 8 | use FluentCart\Framework\Http\Request\Request; |
| 9 | use FluentCart\Framework\Support\Arr; |
| 10 | |
| 11 | class ShippingFrontendController extends Controller |
| 12 | { |
| 13 | public function getAvailableShippingMethods(Request $request) |
| 14 | { |
| 15 | |
| 16 | $timezone = sanitize_text_field($request->get('timezone')); |
| 17 | $state = sanitize_text_field($request->get('state')); |
| 18 | |
| 19 | if ($timezone) { |
| 20 | $countryCode = LocalizationManager::guessCountryFromTimezone($timezone); |
| 21 | } else { |
| 22 | $countryCode = sanitize_text_field($request->get('country_code')); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | if (!$countryCode) { |
| 27 | return [ |
| 28 | 'status' => false, |
| 29 | 'message' => __('Country code is required', 'fluent-cart') |
| 30 | ]; |
| 31 | } |
| 32 | |
| 33 | // Single query: get all applicable methods from both general and class-specific zones |
| 34 | $availableShippingMethods = ShippingMethod::query() |
| 35 | ->whereHas('zone', function ($query) use ($countryCode) { |
| 36 | $query->where(function ($q) use ($countryCode) { |
| 37 | $q->whereIn('region', [$countryCode, 'all']) |
| 38 | ->orWhere('region', 'selection'); |
| 39 | }); |
| 40 | }) |
| 41 | ->where('is_enabled', 1) |
| 42 | ->where(function ($q) use ($state) { |
| 43 | // State filter (same logic as scopeApplicableToCountry) |
| 44 | $isSqlite = defined('DB_ENGINE') && DB_ENGINE === 'sqlite'; |
| 45 | if ($isSqlite) { |
| 46 | $q->where('states', '[]') |
| 47 | ->orWhereNull('states'); |
| 48 | if ($state) { |
| 49 | $escapedState = str_replace(['%', '_'], ['\\%', '\\_'], $state); |
| 50 | $q->orWhere('states', 'LIKE', '%"' . $escapedState . '"%'); |
| 51 | } |
| 52 | } else { |
| 53 | $q->whereJsonLength('states', 0); |
| 54 | if ($state) { |
| 55 | $q->orWhereJsonContains('states', $state); |
| 56 | } |
| 57 | } |
| 58 | }) |
| 59 | ->orderBy('amount', 'DESC') |
| 60 | ->with('zone') |
| 61 | ->get(); |
| 62 | |
| 63 | // Post-filter selection zones that don't match this country |
| 64 | $availableShippingMethods = $availableShippingMethods->filter(function ($method) use ($countryCode) { |
| 65 | if (!$method->zone || $method->zone->region !== 'selection') { |
| 66 | return true; |
| 67 | } |
| 68 | return $method->zone->appliesToCountry($countryCode); |
| 69 | })->values(); |
| 70 | |
| 71 | if (!$availableShippingMethods || $availableShippingMethods->isEmpty()) { |
| 72 | $settingView = '<div class="fct-empty-state">' |
| 73 | . esc_html__('No shipping methods available for this address.', 'fluent-cart'); |
| 74 | |
| 75 | if (current_user_can('manage_options')) { |
| 76 | $settingsPageUrl = admin_url('admin.php?page=fluent-cart#/settings/shipping'); |
| 77 | |
| 78 | $settingsLink = '<a href="' . esc_url($settingsPageUrl ?? '') . '" target="_blank">' . esc_html__('Activate from settings.', 'fluent-cart') . '</a>'; |
| 79 | |
| 80 | $settingView .= ' ' . $settingsLink; |
| 81 | } |
| 82 | |
| 83 | $settingView .= '</div>'; |
| 84 | |
| 85 | |
| 86 | return [ |
| 87 | 'status' => false, |
| 88 | 'country_code' => $countryCode, |
| 89 | 'view' => $settingView |
| 90 | ]; |
| 91 | } |
| 92 | |
| 93 | return [ |
| 94 | 'available_shipping_methods' => $availableShippingMethods, |
| 95 | 'country_code' => $countryCode |
| 96 | ]; |
| 97 | } |
| 98 | |
| 99 | public function getShippingMethodsListView(Request $request) |
| 100 | { |
| 101 | $availableShippingMethods = $this->getAvailableShippingMethods($request); |
| 102 | $shippingMethods = Arr::get($availableShippingMethods, 'available_shipping_methods'); |
| 103 | $countryCode = Arr::get($availableShippingMethods, 'country_code'); |
| 104 | $status = Arr::get($availableShippingMethods, 'status'); |
| 105 | |
| 106 | if ($status === false) { |
| 107 | return $availableShippingMethods; |
| 108 | } |
| 109 | |
| 110 | ob_start(); |
| 111 | do_action('fluent_cart/views/checkout_page_shipping_method_list', [ |
| 112 | 'shipping_methods' => $shippingMethods |
| 113 | ]); |
| 114 | $content = ob_get_clean(); |
| 115 | |
| 116 | return $this->sendSuccess( |
| 117 | [ |
| 118 | 'status' => true, |
| 119 | 'view' => $content, |
| 120 | 'country_code' => $countryCode, |
| 121 | ] |
| 122 | ); |
| 123 | |
| 124 | } |
| 125 | |
| 126 | public function getCountryInfo(Request $request): \WP_REST_Response |
| 127 | { |
| 128 | $timezone = sanitize_text_field($request->get('timezone')); |
| 129 | |
| 130 | if ($timezone) { |
| 131 | $countryCode = LocalizationManager::guessCountryFromTimezone($timezone); |
| 132 | } else { |
| 133 | $countryCode = sanitize_text_field($request->get('country_code')); |
| 134 | } |
| 135 | |
| 136 | $states = LocalizationManager::getInstance()->statesOptions($countryCode); |
| 137 | $addressLocale = LocalizationManager::getInstance()->addressLocales($countryCode); |
| 138 | |
| 139 | return $this->sendSuccess([ |
| 140 | 'country_code' => $countryCode, |
| 141 | 'states' => $states, |
| 142 | 'address_locale' => $addressLocale |
| 143 | ]); |
| 144 | } |
| 145 | } |
| 146 |