试图在 laravel 中编写添加到购物车脚本。创建了一个名为 cart 的类,创建了一个产品控制器,我在其中使用数组 additem 获取产品详细信息,包括产品名称、数量、价格
class Cart
{
public $items;
public $totalQuantity;
public $totalPrice;
public function __construct($prevCart)
{
if ($prevCart != null) {
$this->items = $prevCart->items;
$this->totalQuantity = $prevCart->totalQuantity;
$this->totalPrice = $prevCart->totalPrice;
} else {
$this->items = [];
$this->totalQuantity = 0;
$this->totalPrice = 0;
}
}
public function additem($id, $product)
{
$price = (int)str_replace('$', '', $product->price)
//item already exists
if (array_key_exists($id, $this->items)) {
$productToAdd = $this->items[$id];
$productToAdd['quantity']++;
} else {
$productToAdd = ['quantity' => 1, 'price' => $price, 'date' => $product];
}
$this->items[$id] = $productToAdd;
$this->totalQuantity++;
$this->totalPrice = $this->totalPrice + $price;
}
}
它显示以下错误:
语法错误,意外的“if”(T_IF)
一只名叫tom的猫