Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Shopping Cart Code


winstanley_john

Recommended Posts

Posted

Hello All at oscommerce!

 

I needed to change the way products where treated in the shopping cart and had to play with the contents array that acts as data storage for the shopping cart.

 

See this line

$this->contents[] = array($products_id);

in catalog\includes\classes\shopping_cart.php (about line 90)

 

 

if ($this->in_cart($products_id)) {
       $this->update_quantity($products_id, $qty, $attributes);
     } else {
       $this->contents[] = array($products_id);
       $this->contents[$products_id] = array('qty' => $qty);

 

The line indicated adds a new element to the array at the end of the contents array. The line after it adds the product to the correct position in the array.

 

Why does the first line exist?

 

Running this test script and observing the outcome shows that this mechanism adds too much data to the cart.

 

$products_id = array(10,12,14,16);
$qty = array(10,12,14,16);
$contents = array();

$contents[] = array($products_id[0]); 
$contents[$products_id[0]] = array('qty' => $qty[0]);
$contents[] = array($products_id[1]); 
$contents[$products_id[1]] = array('qty' => $qty[1]);
$contents[] = array($products_id[2]); 
$contents[$products_id[2]] = array('qty' => $qty[2]);
$contents[] = array($products_id[3]); 
$contents[$products_id[3]] = array('qty' => $qty[3]);


echo "<pre>";
print_r($contents);
echo "</pre>";

 

The outcome is

Array
(
   [0] => Array
       (
           [0] => 10
       )

   [10] => Array
       (
           [qty] => 10
       )

   [11] => Array
       (
           [0] => 12
       )

   [12] => Array
       (
           [qty] => 12
       )

   [13] => Array
       (
           [0] => 14
       )

   [14] => Array
       (
           [qty] => 14
       )

   [15] => Array
       (
           [0] => 16
       )

   [16] => Array
       (
           [qty] => 16
       )

)

 

At the end of the method the content array is cleaned to "remove the extra value".

Should this value be there atall?

Archived

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

×
×
  • Create New...