Getting Individual Line Item Discounts in Your Laravel E-commerce Application

Stefan Bogdanescu
Founder & Senior Architect · 14 August 2026

In your e-commerce application built using Laravel, it’s common to need discounts applied to individual cart line items rather than the entire cart total. This allows you to offer targeted promotions and pricing strategies for specific products. In this post, we’ll guide you through the process of calculating and retrieving discounts on a per-item basis.

Understanding Line Item Discounts

Before diving into code examples, let’s clarify what we mean by “line item discounts.” These are promotional offers applied specifically to individual items in your customer’s cart. For instance, you might want to offer 20% off the second shirt purchased or a flat rate reduction on specific branded items.

Calculating Discounts for Each Item

To achieve this, you’ll need to calculate the discount amount for each product added to the cart. The calculation will depend on your business logic and pricing structure.

Let’s assume you have a simple 10% discount applied to all products of brand “Laravel.”

// Calculate 10% discount
$discountAmount = $price * 0.10;

Implementing in the Cart Controller

In your CartController, when adding an item to the cart, you should calculate and store the discounted price for each line item.

public function addToCart(Request $request)
{
    // Retrieve product data
    $product = Product::findOrFail($request->get('id'));

    // Calculate discount if applicable
    if ($product->brand === 'Laravel') {
        $discountedPrice = $product->price * 0.9;
    } else {
        $discountedPrice = $product->price;
    }

    // Add to cart with discounted price
    $cart = session()->get('cart');
    if (!$cart) {
        $cart = [
            0 => [
                "id" => $product->id,
                "name" => $product->name,
                "quantity" => 1,
                "price" => $discountedPrice,
                "total" => $discountedPrice
            ]
        ];
    } else {
        if (isset($cart[$request->get('id')])) {
            $cart[$request->get('id')]['quantity']++;
            $cart[$request->get('id')]['total'] = $cart[$request->get('id')]['price'] * $cart[$request->get('id')]['quantity'];
        } else {
            $cart[$request->get('id')] = [
                "id" => $product->id,
                "name" => $product->name,
                "quantity" => 1,
                "price" => $discountedPrice,
                "total" => $discountedPrice
            ];
        }
    }

    session()->put('cart', $cart);

    return redirect()->back()->with('success', 'Product added to cart successfully.');
}

Retrieving Discounted Line Items

To display the line items with their discounted prices, you can retrieve the cart session data in your blade views.

@if(session('cart'))
  @foreach (session('cart') as $item)
    <tr>
      <td>{{ $item['name'] }}</td>
      <td>{{ $item['price'] }}</td> <!-- Discounted price -->
      <td>{{ $item['quantity'] }}</td>
      <td>{{ $item['total'] }}</td> <!-- Total after discount -->
    </tr>
  @endforeach
@endforeach

Conclusion

Calculating and retrieving discounts for individual cart line items requires a systematic approach to pricing, discount calculation, and storage. By following the examples provided in this post, you can implement targeted promotions and display accurate, discounted prices for each product in your customer’s cart.

Remember that this is just a basic example; your actual implementation may require more complex logic based on your specific business rules and discount structures. Always ensure your code is well-documented and maintainable.

For a deeper dive into Laravel e-commerce development, don’t forget to check out our comprehensive guides available at LaravelCompany.com.

Our network