Ecommerce Components

Shopping Cart Component using Laravel Livewire

Shopping Cart Component using Laravel Livewire

UI Stack: Tailwind CSS

Add Product to Cart

Shopping Cart

Items in your shopping cart

  • Your cart is empty

Order summary

Subtotal
$0.00
Shipping
$5.00
Tax
$0.00
Order total
$5.00

Code

Sorry, this component's code is restricted
Sorry, this component's code is restricted
Sorry, this component's code is restricted
Sorry, this component's code is restricted
Sorry, this component's code is restricted
Sorry, this component's code is restricted

1. Run the migration to create the cart_items table:

php artisan migrate

2. Copy the component files:

  • Copy ShoppingCart.php to your app/Livewire directory
  • Copy shopping-cart.blade.php to your resources/views/livewire directory
  • Copy CartItem.php to your app/Models directory

Integration

To integrate this component into your Laravel project, use the following Livewire directive:

@livewire('shopping-cart')

For testing purposes, you can also include the test component:

@livewire('add-to-cart-test')

Usage Instructions

The shopping cart automatically manages items based on the user's session. Users can:

  • Update item quantities using the dropdown selector
  • Remove items using the X button
  • View real-time price calculations
  • Proceed to checkout when ready

Configuration Options

The component includes several configurable options:

public $shipping = 5.00;    // Default shipping cost
private $taxRate = 0.08;    // Tax rate (8% by default)

Customization Possibilities

The component can be customized in several ways:

1. Tax Calculation:

private function calculateTotals()
{
    $this->tax = round($this->subtotal * $this->taxRate, 2);
}

2. Checkout Process:

public function checkout()
{
    // Add your custom checkout logic here
    $this->dispatch('checkout-initiated', [
        'total' => $this->total,
        'items' => $this->cartItems->count()
    ]);
}

3. Session Management:

public function mount()
{
    $this->sessionId = session()->get('cart_session_id', Str::random(32));
    session()->put('cart_session_id', $this->sessionId);
}

Additional Features

  • Real-time quantity updates
  • Dynamic price calculations
  • Tax and shipping handling
  • Session-based cart management
  • Support for guest and authenticated users
  • Responsive design with Tailwind CSS

Requirements

  • Laravel 10.x or higher
  • Livewire 3.x
  • Alpine.js
  • Tailwind CSS

Ecommerce Product Filter using Livewire and TailwindCSS

Ecommerce Product Filter using Livewire and TailwindCSS

UI Stack: Tailwind CSS

Filters

Filters , active

Code

Sorry, this component's code is restricted
Sorry, this component's code is restricted
Sorry, this component's code is restricted
Sorry, this component's code is restricted
Sorry, this component's code is restricted
Sorry, this component's code is restricted

Usage


        @livewire('tailwind-product-filter')
    

Ecommerce Product Filter using Laravel Livewire

Product filter and sort using livewire with query parameter update.

UI Stack: Bootstrap 5

16 Items found
Loading...
Loading...
Loading...
Noot Kids K11

Noot Kids K11

$13.99

Add to cart
Beats Earbuds

Beats Earbuds

$35.63

Add to cart
Beats Solo3

Beats Solo3

$170.49

Add to cart
Samsung Galaxy Buds

Samsung Galaxy Buds

$179.99

Add to cart
Panasonic Lightweight

Panasonic Lightweight

$6.99

Add to cart
Amazon Basics In-Ear

Amazon Basics In-Ear

$10.99

Add to cart

Code
Please login to View/Download the Code

Sorry, this component's code is restricted
Sorry, this component's code is restricted

Usage


      @livewire('product-filter')
    

Documentation

This is a simple e-commerce filter component using Laravel Livewire. Let's break down this component step by step.

Rendering Initial Products


return view('livewire.product-filter',[
            'products' => DummyProduct::filter($this->filters)->orderBy($this->orderBy['key'], $this->orderBy['direction'])->paginate(12),
        ]);

By default we get all products from the database, paginate the results and apply the default ordering of created_at date.

Filtering Products

In this demo I am using a package named Tucker-Eric/EloquentFilter. This package requires you to add an additional scope method named filter to your Eloquent query and pass in all the filters in the array as the parameter.

DummyProduct::filter($this->filters)

Initially all the filters are empty.


   [
        'price' => [],
        'colors' => [],
        'type' => [],
        'connectionType' => [],
        'brand' => [],
        'rating' => []
   ];

We have used livewire's wire:model to bind these properties to different filters on the front end. When the user selects a value on the backend it updates the filters array and hence Products are filtered. Sleek, isn't it?

Query Parameters

One important thing to note in the Component class is:


protected $queryString = ['filters'];

With this whenever the user selects the filter value, it updates the URL's query parameter. This is useful since user may want to share the filtered product URL or bookmark the URL.

However, since filters are an array and only user-changed values goes into the URL. It creates a problem when a user loads the URL along with the query parameter (on reload the filters array will only have the array parameters passed in the URL and not the others defined.)

For this we have applied a hack on the mount method


$this->filters = array_merge($this->filtersToMerge, $this->filters);

Filters on frontend

Showing filter's on the front end is straightforward. We have defined different filter options in the component.


    public array $filterOptions = [
        'prices' => ['0,25', '25,50', '50,100', '100,200', '200'],
        'colors' => ['blue', 'red', 'pink', 'yellow', 'orange', 'dark', 'green', 'purple', 'white'],
        'connection_type' => ['wired', 'wireless'],
        'brands' => ['sony', 'apple', 'bose', 'beats', 'panasonic', 'amazon-basics'],
        'types' => ['earbuds', 'earphones', 'overhead'],
        'ratings' => [4,3,2,1],
    ];

We just loop through them on the front end and use the filters property to keep track of which values are selected.