我想在提交优惠券代码时更新产品价格,我将优惠券命名为ticket
。这是我的功能:
public function order(Request $request)
{
$products = session('cart');
$ticket_code = Input::get('ticket');
$ticket = Ticket::where('ticket', $ticket_code)->first();
foreach($products as $p){
$price = $p['price'];
}
if(count($products) && $ticket['max'] > $ticket['used']) {
$subtotal = [$price*70/100];
$ticket->used += 1;
$ticket->save();
foreach($products as $p){
$p['price'] = $subtotal;
$p->save();
}
flash()->success('Kuponas sėkmingai panaudotas!');
return view('cart.order')->with(array(
'products' => $products,
'subtotal' => $subtotal,
));
}
else {
$finalTotal = 0;
$subtotal = [];
return view('cart.order')->with(array(
'products' => $products,
'subtotal' => $subtotal,
'finalTotal' => $finalTotal
));
}
}
此代码无效:
foreach($products as $p){
$p['price'] = $subtotal;
$p->save();
}
我收到错误:Call to a member function save() on array
没有foreach
ticket
本身就可以了。它会在提交时更改价格,但仅限于该页面。然后,当我转到付款网站时,价格仍会显示,但未使用ticket
。
工作解决方案
foreach($products as &$item) {
$item['price'] = $item['price']*70;
$item['price'] = $item['price'] / 100;
}
Session::put('cart', $products);
答案 0 :(得分:1)
此工作
foreach($products as &$item) {
$item['price'] = $item['price']*70;
$item['price'] = $item['price'] / 100;
}
Session::put('cart', $products);