Charge shipping as percentage of subtotal

Stefan Bogdanescu
Founder & Senior Architect · 22 September 2026

Calculating Shipping Cost as a Percentage of Subtotal in Laravel

Managing shipping costs dynamically based on the cart subtotal is a common requirement for e-commerce applications. In this post, we’ll demonstrate how to calculate shipping charges as a percentage of the total order amount using Laravel.

Understanding the Requirement

The client wants to allow admin configuration of the shipping percentage and apply that rate to the order subtotal. For example:

Admin-defined Shipping Percentage: 10% Order Subtotal: $100 Calculated Shipping Cost: $10

Setting Up the Admin Configuration

First, let’s create an admin panel where we can manage the shipping percentage. We’ll use Laravel’s built-in authentication and Blade templating.

In your config directory, add a new file named shipping.php. Define the default shipping percentage in this configuration file:

<?php

return [
    'percentage' => 10,
];

Next, create an admin route to manage this value. In your routes/admin.php file:

Route::get('/shipping-settings', function() {
    return view('admin.shipping_settings');
})->middleware('auth:web');

Now, let’s build the Blade template for the shipping settings page (resources/views/admin/shipping_settings.blade.php).

@extends('layouts.admin')

@section('content')
<div class="container">
    <h2>Shipping Settings</h2>
    <form action="{{ route('update.shipping') }}" method="POST">
        @csrf
        <div class="form-group">
            <label for="shipping_percentage">Shipping Percentage (%):</label>
            <input type="number" name="shipping_percentage" value="{{ config('shipping.percentage') }}" required>
        </div>
        <button type="submit" class="btn btn-primary">Update Settings</button>
    </form>
</div>
@endsection

Updating the Shipping Percentage

We need to create a route and controller method to update the shipping percentage.

In your routes/admin.php file:

Route::post('/update-shipping', [ShippingController::class, 'updateSettings'])->name('update.shipping');

Create a ShippingController class in app/Http/Controllers:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Config;

class ShippingController extends Controller
{
    public function updateSettings(Request $request)
    {
        Config::set('shipping.percentage', $request->input('shipping_percentage'));
        return redirect()->back()->with('success', 'Shipping settings updated successfully.');
    }
}

Calculating the Shipping Cost

Now, let’s calculate the shipping cost in your order processing logic. Assume you have an OrderController that creates new orders.

namespace App\Http\Controllers;

use Config;
use Illuminate\Http\Request;
use App\Models\Order;

class OrderController extends Controller
{
    public function create(Request $request)
    {
        // Calculate subtotal
        $subtotal = $request->input('subtotal');

        // Get shipping percentage from config
        $shippingPercentage = Config::get('shipping.percentage', 0);

        // Calculate shipping cost as a percentage of the subtotal
        $shippingCost = ($subtotal * $shippingPercentage) / 100;

        // Create the order with shipping cost
        Order::create([
            'subtotal' => $subtotal,
            'shipping_cost' => $shippingCost,
            // Other fields...
        ]);

        return redirect()->route('orders.thank-you', ['order_id' => $order->id]);
    }
}

Conclusion

In this post, we’ve demonstrated a practical way to manage and calculate shipping costs as a percentage of the order subtotal in a Laravel application.

By using Laravel’s configuration system and routing, we’ve created an admin interface for managing the shipping percentage. We then integrated this value into the order processing logic to calculate the shipping cost dynamically.

Remember to replace the placeholders with your actual model names and route paths.

For more advanced e-commerce features like tax calculations or multi-tiered shipping rates, consider using a dedicated package or following our guide on building an e-commerce platform for a comprehensive approach.

Our network