I am developing a shopping cart website. On my cart page, the total price of an item should be updated as per the quantity of that item. https://github.com/darryldecode/laravelshoppingcart I installed this shopping cart package.
How can I update the total price of an item according to its quantity?
CartController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Category;
use Darryldecode\Cart\Cart;
class CartController extends Controller
{
public function index()
{
// $cartItems = \Cart::session(auth()->id())->getContent();
return view ('cart');
}
public function show($id)
{
$product = Product::find($id);
return view('cart')->with(compact('product'));
}
public function update($rowId)
{
\Cart::session(auth()->id())->update($rowId, [
'quantity' => [
'relative' => true,
'value'=> request('quantity')
]
]);
return back();
}
public function destroy($itemId)
{
\Cart::session(auth()->id())->remove($itemId);
return back();
}
public function addtocart(Product $product)
{
\Cart::session(auth()->id())->add(array(
'id' => $product->id,
'name' => $product->prod_name,
'price' => $product->prod_price,
'quantity' => 1,
'attributes' => array(),
'associatedModel' => $product
));
return redirect()->back();
}
}
cart.blade.php
<form class="mb-4" action="#" method="post">
<table class="table" cellspacing="0">
<thead>
<tr>
<th class="product-remove"> </th>
<th class="product-name">Product</th>
<th class="product-price">Price</th>
<th class="product-quantity w-lg-15">Quantity</th>
<th class="product-subtotal">Total</th>
</tr>
</thead>
<tbody>
@foreach(\Cart::session(auth()->id())->getContent() as $items)
<tr>
<td class="text-center">
<a href="">x</a>
</td>
<td data-title="Product">
<a href="#" class="text-gray-90"></a
</td>
<td data-title="Price">
<span class="">LKR .00</span>
</td>
<td data-title="Quantity">
<span class="sr-only">Quantity</span>
<!-- Quantity -->
<div class="border rounded-pill py-1 width-122 w-xl-80 px-3 border-color-1">
<div class="js-quantity row align-items-center">
<div class="col">
<input class="js-result form-control h-auto border-0 rounded p-0 shadow-none" name="quantity" type="text" value="">
</div>
<div class="col-auto pr-1">
<a class="js-minus btn btn-icon btn-xs btn-outline-secondary rounded-circle border-0" href="javascript:;">
<small class="fas fa-minus btn-icon__inner"></small>
</a>
<a class="js-plus btn btn-icon btn-xs btn-outline-secondary rounded-circle border-0" href="javascript:;">
<small class="fas fa-plus btn-icon__inner"></small>
</a>
</div>
</div>
</div>
<!-- End Quantity -->
</td>
<td data-title="Total">
<span class="">
</span>
</td>
@endforeach
<td>
</td>
</tr>
<tr>
<td colspan="6" class="border-top space-top-2 justify-content-center">
<div class="pt-md-3">
<div class="d-block d-md-flex flex-center-between">
<div class="mb-3 mb-md-0 w-xl-40">
<!-- Apply coupon Form -->
<form class="js-focus-state">
<label class="sr-only" for="subscribeSrEmailExample1">Coupon code</label>
<div class="input-group">
<input type="text" class="form-control" name="text" id="subscribeSrEmailExample1" placeholder="Coupon code" aria-label="Coupon code" aria-describedby="subscribeButtonExample2" required>
<div class="input-group-append">
<button class="btn btn-block btn-dark px-4" type="button" id="subscribeButtonExample2"><i class="fas fa-tags d-md-none"></i><span class="d-none d-md-inline">Apply coupon</span></button>
</div>
</div>
</form>
<!-- End Apply coupon Form -->
</div>
<form action="">
<div class="d-md-flex">
<button type="submit" class="btn btn-soft-secondary mb-3 mb-md-0 font-weight-normal px-5 px-md-4 px-lg-5 w-100 w-md-auto">Update cart</button>
<a href="../shop/checkout.html" class="btn btn-primary-dark-w ml-md-2 px-5 px-md-4 px-lg-5 w-100 w-md-auto d-none d-md-inline-block">Proceed to checkout</a>
</div>
</form>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</form>
web.php
Route::get('/cart', 'CartController@index')->name('cart.index')->middleware('auth');;
Route::get('/cart/{cartItems}', 'CartController@add')->name('cart.add')->middleware('auth');
Route::get('/cart/destroy/{itemId}', 'CartController@destroy')->name('cart.destroy')->middleware('auth');
Route::get('/cart/update/{itemId}', 'CartController@update')->name('cart.update')->middleware('auth');
Route::get('/add-to-cart/{product}','CartController@addtocart')->name('addToCart');
from How to update the total price of an item according to its quantity?
No comments:
Post a Comment