yonka's avatar
Level 2

I want to change Qty and Price each item seperately using livewire and calculate Subtotal. I am using bumbummen99/shoppingcart

Hi, I am using bumbummen99/shoppingcart with livewire, so I want to be able to change quantity and Price each item I added to cart and also at the same time to dynamically check this Quantity is not more than Qty in my stock.

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve the functionality of updating the quantity and price of each item in the cart using Livewire, while also ensuring that the quantity does not exceed the available stock, you can follow these steps:

  1. Set Up Your Livewire Component:

    Create a Livewire component that will handle the cart operations. This component will manage the cart items, update quantities, prices, and calculate the subtotal.

  2. Component Logic:

    In your Livewire component, you will need to define properties for the cart items and methods to update the quantity and price. You will also need a method to calculate the subtotal.

    Here's a basic example:

  3. Blade View:

    Create a Blade view for your Livewire component to display the cart items and allow users to update the quantity and price.

    <div>
        @foreach($cartItems as $item)
            <div>
                <span>{{ $item['name'] }}</span>
                <input type="number" wire:change="updateQuantity('{{ $item['rowId'] }}', $event.target.value)" value="{{ $item['qty'] }}">
                <input type="number" wire:change="updatePrice('{{ $item['rowId'] }}', $event.target.value)" value="{{ $item['price'] }}">
                <span>{{ $item['subtotal'] }}</span>
            </div>
        @endforeach
    
        <div>
            <strong>Subtotal: </strong> {{ $subtotal }}
        </div>
    
        @if(session()->has('error'))
            <div class="alert alert-danger">
                {{ session('error') }}
            </div>
        @endif
    </div>
    
  4. Handling Stock Validation:

    Ensure that the getStock method correctly fetches the stock level from your database. This method is crucial for validating that the requested quantity does not exceed available stock.

  5. Testing:

    Test the component to ensure that it updates the cart items correctly and that the subtotal is recalculated dynamically. Also, verify that the stock validation works as expected.

By following these steps, you should be able to dynamically update the quantity and price of items in your cart using Livewire, while also ensuring that the quantity does not exceed the available stock.

Please or to participate in this conversation.