7 Replies - 519 Views - Last Post: 08 November 2019 - 07:16 PM Rate Topic: -----

#1 makamo66   User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 104
  • Joined: 04-February 09

Update shopping cart by adding quantities together if the same product

Posted 05 November 2019 - 05:32 PM

This is the code that I'm using for a shopping cart in order to update the quantity if the same product id is chosen.

$cart_items is a two-dimensional session array composed of the id session array and the quantity session array. While looping through $cart_items, I create a temporary array $temp_array so that I can execute some logic on the array without changing the normal output. $temp_array is the $cart_items array without the last items which would be the $_REQUEST id and the $_REQUEST quantity. So the two $_REQUESTs are popped off with array_pop and then the remaining $_SESSIONs are compared to the $_REQUEST id with

$j = array_search($_REQUEST["element_id_$i"], $temp_array);
$j is now the key that corresponds to the id in $temp_array where the $_REQUEST id has been found.

if( $j==$temp_array[$i])

If $j is the key of the item in $temp_array, then the $_REQUEST is unset and the quantities for the item are added together.

There are two problems with this code. First of all the var_dump of the $temp_array is always an empty array but I am expecting to get an array with just the last element popped off. Secondly,
I get the error "Undefined offset" for the $i variable in this line of the code: if( $j==$temp_array[$i])


foreach($cart_items as $item) {
foreach($item["id"] as $key => $val) {
foreach($item["quantity"] as $i => $value) {
	if ($key == $i){
	
$temp_array = $cart_items;
$array = array_pop($temp_array);
var_dump($temp_array);

if (isset($_REQUEST["element_id_$i"]) && isset($_REQUEST["quantity_$i"])&& isset($value)){
	
$j = array_search($_REQUEST["element_id_$i"], $temp_array);
if( $j==$temp_array[$i]) {
echo "<b>SAME PRODUCT ADDED</b>";
$value += $_REQUEST["quantity_$i"];
unset($_REQUEST["element_id_$i"]);
}}}}}}



Is This A Good Question/Topic? 0
  • +

Replies To: Update shopping cart by adding quantities together if the same product

#2 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2468
  • View blog
  • Posts: 7,506
  • Joined: 15-January 14

Re: Update shopping cart by adding quantities together if the same product

Posted 05 November 2019 - 06:18 PM

You're probably going to need to do a print_r on $cart_items so we know what we're working with.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,946
  • Joined: 08-August 08

Re: Update shopping cart by adding quantities together if the same product

Posted 05 November 2019 - 07:15 PM

Classes make organizing easier.
class cart {
    private $items;

    public function __construct($items = [])
    {
        $this->items = $items;
    }
    
    public function addItem($id, $quantity)
    {
        if(empty($this->items[$id])) {
            $this->items[$id] = $quantity;
        } else {
            $this->items[$id] += $quantity;
        }
    }
    
    public function removeItem($id)
    {
        if(array_key_exists($id, $this->items)) {
            unset($this->items[$id]);
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 makamo66   User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 104
  • Joined: 04-February 09

Re: Update shopping cart by adding quantities together if the same product

Posted 07 November 2019 - 03:08 PM

View PostArtificialSoldier, on 05 November 2019 - 06:18 PM, said:

You're probably going to need to do a print_r on $cart_items so we know what we're working with.


$temp_array = $cart_items;
$array = array_pop($temp_array);
echo "<br /><br />";
print_r($temp_array);
echo "<br /><br />";
print_r($cart_items);


After 3 items are added to the cart, prints out:

Array ( )

Array ( [cart_item] => Array ( [id] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [quantity] => Array ( [0] => 1 [1] => 1 [2] => 3 ) ) )
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2468
  • View blog
  • Posts: 7,506
  • Joined: 15-January 14

Re: Update shopping cart by adding quantities together if the same product

Posted 07 November 2019 - 05:00 PM

OK, and what about before using array_pop or any other actions on the array? I want to see how the array starts.

Also, what do you expect to happen when you use array_pop on an associative array?
Was This Post Helpful? 0
  • +
  • -

#6 makamo66   User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 104
  • Joined: 04-February 09

Re: Update shopping cart by adding quantities together if the same product

Posted 08 November 2019 - 06:14 AM

If you were working on a PHP shopping cart using SESSION variables for the cart item's product id and quantity, how would you update the cart if the same product were chosen? If the same product id was posted, how would you add the quantities and show just the same product?
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2468
  • View blog
  • Posts: 7,506
  • Joined: 15-January 14

Re: Update shopping cart by adding quantities together if the same product

Posted 08 November 2019 - 10:27 AM

When someone tries to add a new item to the cart, I'd look to see if it's already there and update the quantity instead.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,946
  • Joined: 08-August 08

Re: Update shopping cart by adding quantities together if the same product

Posted 08 November 2019 - 07:16 PM

View Postmakamo66, on 08 November 2019 - 08:14 AM, said:

...how would you update the cart if the same product were chosen? If the same product id was posted, how would you add the quantities and show just the same product?

Did you not read my post?

Here's an example you can run:
class cart {
    private $items;

    public function __construct($items = [])
    {
        $this->items = $items;
    }

    public function addItem($id, $quantity)
    {
        if(empty($this->items[$id])) {
            $this->items[$id] = $quantity;
        } else {
            $this->items[$id] += $quantity;
        }
    }

    public function removeItem($id)
    {
        if(array_key_exists($id, $this->items)) {
            unset($this->items[$id]);
        }
    }

    public function showItems()
    {
        echo "ID: Quantity<br>";
        foreach($this->items as $id => $item) {
            echo $id.": ".$item."<br>";
        }
    }

}

$items = [
    ['id'=> 10, 'qty'=>1],
    ['id'=> 22, 'qty'=>1],
    ['id'=> 10, 'qty'=>1],
    ['id'=> 33, 'qty'=>11],
    ['id'=> 10, 'qty'=>1],
];

$cart = new cart();
foreach($items as $item) {
    $cart->addItem($item['id'], $item['qty']);
}

$cart->showItems();
$cart->removeItem(33);
$cart->showItems();


My results:

ID: Quantity
10: 3
22: 1
33: 11
ID: Quantity
10: 3
22: 1

This post has been edited by CTphpnwb: 09 November 2019 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1