Jump to content

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.

I know this is cheeky but could you post up a modified version on the update case that would work please :) I did not design the original script and would be here all year otherwise. I would send you $20 via paypal if you would be so kind to do so.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.