Poleciałem na łatwiźnie i znalazłem klasę do obsługi koszyka.
Produkty ładnie wędrują do koszyka, ale chciałbym, aby ilość produktów tego samego produktu była sumowana.
Przedstawmy to tak:
Mamy do kupienia Marchewkę, kupujemy 100 sztuk (1 zł szt.) - do cookies "cart" wędruje ID_PROD - 100 - MARCHEWKA - 1.
Nagle dzwoni żona i mówi że potrzebuje jeszcze 50 marchewek, więc wklepujemy 50 i akceptujemy.
I chciałbym aby w cookies było:
ID_PROD - 150 - MARCHEWKA - 1
Niestety, naszym oczom pokazuje się tylko:
ID_PROD - 50 - MARCHEWKA - 1
Czyli uciekło nam 100 poprzednich marchewek.
Albo jestem już zmęczony i nie myślę logicznie, albo coś tutaj nie gra, proszę o jakąś podpowiedź.
Dołączam tą klasę, której używam, jest tam funkcja updateitem, ale nie za bardzo wiem jak i kiedy jej użyć.
Dziękuję
<?php /* Basket class for e-commerce purpose Version : 0.5 Type:Class Category:Shopping Carts License: GNU General Public License Description: This class provides methods for add, remove, update and remove all items from a basket. The basket works with a session cookie, saving the product ID and quantity in an array for simple accesing it. There's an example in this category... called "Basket example using basket class" */ class basket { var $items; var $empty; function basket() { { { $this->empty=false; } else $this->empty=true; } else $this->empty=true; } function additem($id, $name, $count, $prix) { if ($this->items[$id][1] > 0 ) { $this->items[$id][1]+=$count; } else { $this->items[$id][0]=$name; $this->items[$id][1]=$count; $this->items[$id][2]=$prix; } $this->empty=false; } function removeitem($id) { $tmp=""; // Don't forget to initialize ! if(!$this->empty) { if($x!=$id) $tmp[$x]=$y; }; }; $this->items=$tmp; // Or this will have no effect ! } function resetArray($basket) { } function countItems($basket) { if(!$basket->empty) { { $ant++; } } return $ant; } function sumItems($basket) { if(!$basket->empty) { { $ant = $ant + $y[1]; } } return $ant; } function printItems($b) { if(!$b->empty) { { } } } function updateitem($id,$count){ $this->items[$id][1]=$count; } function removeall(){ $this->empty=true; } } ?>