Forum Moderators: coopster

Message Too Old, No Replies

merging shopping cart values

         

nshack31

8:49 am on Oct 20, 2008 (gmt 0)

10+ Year Member



i have an array to store my cart and an array to add total price...

$cart[] = array('sizeid'=>$sizeid, 'photoid'=>$photoid,'photopath'=>$photopath,'size'=>$size,'price'=>$price,'quantity'=>$quantity);
$totpricearr[] = $price;

i can then output my cart...

foreach ($cart as $key => $v) {
output cart data

the problem i have is quantity - if the user enters 2 in the quantity box it correctly adds this to the cart.. however if the user adds the item once, and then adds it again the item shows up twice in the cart rather than merging the values.

how can i merge it if the same 'sizeid'=>$sizeid, 'photoid'=>$photoid are found in the array more than once?

pseudo..

if in cart array sizeid and photoid both found on same row - add to quantity and remove one of the rows

thanks!

tumr

2:41 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



Instead of

$cart[] = array(etc)

you could consider

if($cart["$uniquelyIdentifyingid"]){
$cart["$uniquelyIdentifyingid"] = array(etc,quantity=>($quantity+$additionalQuantity));
}else{
$cart["$uniquelyIdentifyingId"] = array(etc,quantity=>$quantity);
}

This is pseudocode that, for an item that's already been added to the cart before, simply increments the quantity.

nshack31

8:54 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



im still struggling with this one! put more simply here is my array...

$cart[] = array('sizeid'=>$sizeid,'photoid'=>$photoid,'photopath'=>$photopath,'size'=>$size,'price'=>$price,'quantity'=>$quantity); 

i need to run a foreach loop and if photoid and sizeid are already present i need to increase the quantity and remove the duplicate values

can anbody help? thanks!