Recently I switched to the OOP and am making a shopping cart for the site. Found a basket here - Basket
Cooks are still not very familiar, and in general with the PLO. But I want to understand.
There is a class Cart, in which there are 2 methods responsible for products
public function getItems($for_sql = false) { if ($for_sql) { return implode(',', $this->items); } return $this->items; } public function addItem($id) { $id = (int)$id; $key = array_search($id, $this->items); if (!in_array($id, $this->items)) { array_push($this->items, $id); } Cookie::set('items', serialize($this->items)); } And in the file cart.php I display this way:
echo "My cart: <br>"; foreach ($items as $items) { echo "<b>{$items['name']}</b> | Количество " .count($items['id']). " | <a href='cart.php?action=delete&id={$items['id']}'>Удалить из корзины</a><br>"; }
Tried to display the number of repeated id through count, but also unsuccessfully. Therefore, I ask for help. How to do the following:
How to add the quantity of goods, if this product is already added to the cart? At the moment, only once the product is added, and then the quantity of this product is not added (+ 1 to the quantity)
How to display the total amount using this basket class?
I tried to add the function array_key_exists () to the addItem ($ id) method, but without success. The quantity does not add up.