admin管理员组文章数量:1400123
I am working on a Laravel project where the shipping cost is dynamically calculated based on the selected city. The shipping_address session data is stored correctly and works inside CheckoutController and PaymentHelper, but when I try to retrieve it in PriceHelper.php, it returns null, causing the calculation to fail. What Works (Inside Controllers & Helpers)
Inside CheckoutController, session data is successfully retrieved:
// Retrieve the selected shipping city from session
$shipping_address = Session::get('shipping_address');
$selected_city = ucfirst(strtolower($shipping_address['ship_city'] ?? ''));
// Fetch all unique shipping services
$shipping_services = DB::table('shipping_services')
->where('status', 1)
->select('id', 'title', 'base_price', 'cod_fee', 'handling_fee', 'insurance_percentage')
->distinct()
->get();
$unique_shipping_services = collect($shipping_services)->unique('id');
$shipping_prices = [];
$calculated_shipping_services = []; // Track processed services
foreach ($unique_shipping_services as $service) {
if (in_array($service->id, $calculated_shipping_services)) {
continue;
}
$calculated_shipping_services[] = $service->id;
if ($service->id <= 1) continue; // Ignore Free Delivery
$base_price = (float) $service->base_price;
// Get weighted price based on selected city
$weighted_price = DB::table('shipping_cities')
->where('shipping_service_id', $service->id)
->whereRaw('LOWER(name) = ?', [strtolower($selected_city)])
->value('weighted_price') ?? 0;
$cod_fee = (float) $service->cod_fee;
$handling_fee = (float) $service->handling_fee;
$insurance_fee = ($service->insurance_percentage > 0)
? ((float) $service->insurance_percentage / 100) * $cart_total
: 0;
// Retrieve shipping_id from session or request
$shipping_id = $request->shipping_id ?? Session::get('shipping_id', 0);
$shipping_id = (int) $shipping_id;
// 5% extra cart fee if shipping_id is NOT 1 (not free delivery)
$extra_cart_fee = ($shipping_id !== 1) ? ($cart_total * 5) / 100 : 0;
// Final shipping cost calculation
$shipping_prices[$service->id] = $base_price + $weighted_price + $cod_fee + $handling_fee + $insurance_fee + $extra_cart_fee;
}
This works fine inside the controller. Similarly, the shipping_address session data is correctly retrieved in PaymentHelper.php:
$final_shipping_price = 0;
if ($shipping) {
$base_price = (float) $shipping->base_price;
$shipping_address = Session::get('shipping_address');
$selected_city = ucfirst(strtolower($shipping_address['ship_city'] ?? ''));
$weighted_price = DB::table('shipping_cities')
->where('shipping_service_id', $shipping->id)
->whereRaw('LOWER(TRIM(name)) = ?', [strtolower($selected_city)])
->value('weighted_price') ?? 0;
$cod_fee = (float) $shipping->cod_fee;
$handling_fee = (float) $shipping->handling_fee;
$insurance_fee = ($shipping->insurance_percentage > 0)
? ((float) $shipping->insurance_percentage / 100) * $cart_total
: 0;
// Retrieve shipping_id from session
$shipping_id = $request->shipping_id ?? Session::get('shipping_id', 0);
$shipping_id = (int) $shipping_id;
// 5% extra cart fee if shipping_id is NOT 1 (not free delivery)
$extra_cart_fee = ($shipping_id !== 1) ? ($cart_total * 5) / 100 : 0;
$final_shipping_price = $base_price + $weighted_price + $cod_fee + $handling_fee + $insurance_fee + $extra_cart_fee;
}
In PriceHelper.php, session data does not work and always returns null:
$shipping_address = request()->session()->get('shipping_address', []);
$selected_city = ucfirst(strtolower($shipping_address['ship_city'] ?? ''));
Log::info('Selected City:', ['selected_city' => $selected_city]); // Always logs null
if (!$shipping || !isset($shipping['base_price'])) {
return 0; // No valid shipping details
}
$base_price = (float) $shipping['base_price'];
// Get weighted price
$weighted_price = DB::table('shipping_cities')
->where('shipping_service_id', $shipping['id'])
->whereRaw('LOWER(TRIM(name)) = ?', [strtolower($selected_city)])
->value('weighted_price') ?? 0;
$cod_fee = (float) ($shipping['cod_fee'] ?? 0);
$handling_fee = (float) ($shipping['handling_fee'] ?? 0);
$insurance_fee = ($shipping['insurance_percentage'] ?? 0) > 0
? ((float) $shipping['insurance_percentage'] / 100) * $cart_total
: 0;
// Retrieve shipping_id from request/session
$shipping_id = request()->shipping_id ?? Session::get('shipping_id', 0);
$shipping_id = (int) $shipping_id;
// Apply extra 5% cart fee if shipping is not free
$extra_cart_fee = ($shipping_id !== 1) ? ($cart_total * 5) / 100 : 0;
return $base_price + $weighted_price + $cod_fee + $handling_fee + $insurance_fee + $extra_cart_fee;
What I Have Tried:
Verified that Session::get('shipping_address') works inside controllers.
Logged Session::get('shipping_address') inside PriceHelper.php (returns null).
Attempted using request()->session()->get('shipping_address'), still null.
Checked Laravel session storage (other session data is accessible in controllers but not in helpers).
Tried using Laravel config() to store session data globally, but it didn’t persist.
Attempted passing shipping_address explicitly to PriceHelper methods but faced scope issues.
Questions: Why does Session::get('shipping_address') return null inside PriceHelper.php but works in CheckoutController? What is the best way to make shipping_address globally accessible in helpers like PriceHelper.php?
Expected Outcome: I need PriceHelper.php to correctly retrieve shipping_address from the session, just as it works in CheckoutController and PaymentHelper, so that shipping cost calculations function properly.
版权声明:本文标题:laravel - Why session is not working in pricehelper but working perfectly in paymenthelper - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744235119a2596520.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论