gpt4 book ai didi

php - 如何根据数量更新商品的总价?

转载 作者:行者123 更新时间:2023-12-05 07:02:38 29 4
gpt4 key购买 nike

我正在开发一个购物车网站。在我的购物车页面上,商品的总价应根据该商品的数量进行更新。 https://github.com/darryldecode/laravelshoppingcart 我安装了这个购物车包。

如何根据数量更新商品的总价?

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

                        @foreach(\Cart::session(auth()->id())->getContent() as $items)
<tr>
<form class="mb-4" action="{{route('cart.update',$items->id)}}">

<td data-title="Product">
<a href="#" class="text-gray-90">{{ $items ['name'] }}</a>
</td>

<td data-title="Price">
<span class="">LKR {{ $items ['price'] }}.00</span>
</td>

<td data-title="Quantity">
<span class="sr-only">Quantity</span>
<!-- <form class="mb-4" action="{{route('cart.update',$items->id)}}"> -->
<br>
<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="{{$items->quantity}}">
</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>
<!-- </form> -->
</td>



<!-- 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="number" value="{{$items->quantity}}">
<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>
</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="">
LKR {{Cart::session(auth()->id())->get($items->id)->getPriceSum()}}.00
</span>
</td>

<td>
<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"><i class="fas fa-pen-nib text-primary fa-2x"></i></button>
<a href="{{ route('cart.destroy', $items->id)}}" 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"><i class="fas fa-trash-alt text-danger fa-2x"></i></a>
</td>

<!-- <td>
<div class="d-md-flex">
<form class="mb-4" action="{{route('cart.update',$items->id)}}">
<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>
</form>
</div>
</td> -->
</form>
</tr>
@endforeach

网络.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');

最佳答案

对 Laravel 了解不多,但这个 javascript 解决方案会让您了解如何继续...

function getPriceSum() {
YOUR_ARRAY.reduce((sum, item) => { return sum + (item.price * item.quantity), 0 })
}

关于php - 如何根据数量更新商品的总价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63517799/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com