Jump to content

Problem with update function


adamjblakey

Recommended Posts

Hi,

 

I have a function which updates a cart but it is not working correctly. Basically if i have e.g.

 

2 x Red Shoes

3 x Blue Shorts

 

And then i change the qty of the blue shorts to 4 and pass to the update function it just completely removes the first lot of items in the list e.g. the 2 x red shorts.

 

The data is stored in a session like so:

58:|Red Shoes|14.95,58:|Red Shoes|14.95,56:|Blue Shorts|8.95,56:Size|Blue Shorts|8.95,56:Size|Blue Shorts|8.95

 

Here is the code for the function:

case 'update':
if ($cart) {
	$newcart = '';
	foreach ($_POST as $key=>$value) {
		if (stristr($key,'qty')) {
			$id = str_replace('qty','',$key);
			$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($id != $item) {
					if ($newcart != '') {
						$item = '';
						$newcart .= ','.$item;
					} else {
						$item = '';
						$newcart = $item;
					}
				}
			}
			for ($i=1;$i<=$value;$i++) {
				if ($newcart != '') {
					$newcart .= ','.$id;
				} else {
					$newcart = $id;
				}
			}
		}
	}
}
$cart = preg_replace("/_/i", " ", preg_replace("/(\d+)_(\d+)/i", "$1.$2", $newcart));


break;
}

 

Thanks in advance,

Adam

Link to comment
https://forums.phpfreaks.com/topic/172682-problem-with-update-function/
Share on other sites

You need a more logical approach to your data storage.

Products in the cart would be best stored as an associative array with the index being the product id. When you add/modify a product in/to the cart the initial lookup should be against its id.

 

Add to cart

1. Check if product is in cart

i. If product is in cart, increase quantity by quantity value + current quantity

end

2. Add product to cart

 

// cart
array[123] = array('name' => 'red shoes',
	   'quantity' => 10,
	   'price' => 9.99);
array[127] = array('name' => 'blue jeans',
	   'quantity' => 1,
	   'price' => 19.99);

 

I would store a cart object in your session. Use member functions to modify the cart.

 

class cart {
private $cartContents;

// check if a product is in the cart
public function isInCart($productId) {

}
// add product to cart
public function addToCart($productId, $quantity = 1) {

}
// update product quantity
private function updateQty($productId, $quantity) {

}
// remove from cart
public function removeFromCart($productId) {

}
// get cart total
public function cartTotal() {

}
// empty cart
public function emptyCart() {
	$this->cartContents = array();
}
}
[code]

Hi,

 

Thank you for your reply, this would be a better way to handle things but at the moment everything is all set-up and working fine apart from this minor detail. As this is currently on a working website my main concern is to get this problem fixed as fast as i can then i can look at improving the functionality. Do you have any ideas on why this problem would be occurring?

 

Cheers,

Adam

Because your cart is being cleared as soon as the switch case is update.

$newcart = '';

Looking at this code you would have to rebuild the cart every time an update is made. i.e. All current products must be in the post array.

I cannot see how your implementation is working fine as it is fundemetally flawed logic. As I stated when you update a quantity you need to check the current quantity of the product. You must obtain its value from the cart then increase or decreas by the desired quantity, not rebuild the cart from scratch.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.