Jump to content

Recommended Posts

I'm doing something like that. My quantity is updating when I add the same product but I need to delete the product that's the last item in the SESSION array. So far I've tried array_pop and unset but I can't get rid of the non-updated product.

These two don't work to remove the last product added to the session:

unset($_SESSION["cart_item"]["element_id"]);
array_pop($_SESSION["cart_item"]);

30 minutes ago, makamo66 said:

I'm doing something like that

Doesn't sound like you are doing anything like that.

In my code, if the product isn't in the cart it adds it, with the quantity. If the product is already in the cart it updates the quantity.

No removals required (unless, of course, the user wants to cancel)

This is what I'm using and it's not working:

foreach($cart_items as $item) {
foreach($item["id"] as $key => $val) {
foreach($item["quantity"] as $i => $value) {
if ($key == $i){    
if(empty($item["quantity"])){
  $item["quantity"] = $value;
} elseif (isset($_REQUEST["quantity_$i"]) ) {
$item["quantity"] = $value + $_REQUEST["quantity_$i"];
$value = $item["quantity"];
 echo "Same product";
}}}}
 
 

It looks to me that your making things harder than they should be. Here's what I would do for a cart array:

	<?php 
	$cart_items = array("pants"=>"3", "shirt"=>"4",  "shoe"=>"5", "banana"=>"13",  "hat"=>"1"); 
	foreach ($cart_items as $item => $quantity){ 
	    echo "Item name is ".$item." and he wants this amount ".$quantity."\n"; 
	}
	?> 
Edited by NotSunfighter

And I'd remove objects from the array like this

	<?php 
	$cart_items = array("pants"=>"3", "shirt"=>"4",  "shoe"=>"5", "banana"=>"13",  "hat"=>"1"); 
	 
	if (($key = array_search('4', $cart_items)) !== false) {
	    unset($cart_items[$key]);
	}
	 
	foreach ($cart_items as $item => $quantity){ 
	    echo "Item name is ".$item." and he wants this amount ".$quantity."\n"; 
	}
	?> 
	

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.