Welcome Guest, Not a member yet? Register   Sign In
Cart: updating the "options" array
#1

[eluser]stef25[/eluser]
The options array in the Cart class exists to store additional data that does not include the 4 default values of id, quantity, price and name.

I have a few extra fields, one of which is "duration" (im using the cart to build a reservation system for items that can be rented)

When I run the code below, the cart->update() always fails. Is it possible to update the options array?

Code:
$product['duration'] = $this->input->post('new_duration');        

            $cart_data = array(
               'id'      => $product['prod_code'],
               'qty'     => $this->input->post('new_quantity'),
               'price'   => $price,
               'name'    => $product['title'],
               'options' => $product
            );
    
            if( $this->cart->update($cart_data) ) die('YES');
            else die('NO');

The cart_data array is below, which should be all ok? I know I'm storing duplicate values in the options array but I don't think this matters ...

Code:
Array
(
    [id] => 6005
    [qty] => 666
    [price] => 270.0
    [name] => bla
    [options] => Array
        (
            [id] => 1
            [title] => bla
            [url_title] => bla
            [type] => vente
            [prod_code] => 6005
            [monthly_price] => 270.0
            [price_1_day] =>
            [price_2_day] =>
            [transport] => 4
            [guarantee] => B
            [notes] =>
            [status] => open
            [entry_date] => 0
            [parent_cat_fk] => 4
            [child_cat_fk] => 19
            [duration] => 666
        )

)

Why is this failing?
#2

[eluser]stef25[/eluser]
OK, I think it's because I'm not passing the rowid value ...
#3

[eluser]packetfox[/eluser]
Hello, have you solved this issue? I also would like to update values stored in the options array, but i think it will not work without a custom piece of code that extends the cart class. From what i can see, the cart->update() function only takes care updating the Value for the Quantity Field, but leaves what is in the options array untouched.
#4

[eluser]stef25[/eluser]
It's not possible to update the options array; I made some modifications in the cart class, but modifying the core is not really recommended ...
#5

[eluser]packetfox[/eluser]
I know its not recommended, but maybe a quick fix before i get down extending the class. Is it convenient / possible to share your code modification?

Thanks either way and have a great day.
#6

[eluser]stef25[/eluser]
Actually I just modified it so I can update just one option specific to my application, that is the "duration" field. I'm using the cart to "reserve" items for rental that are then picked up at the stores.

Code:
if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty'])

changed to

Code:
if ($this->_cart_contents[$items['rowid']]['qty'] == $items['qty'] AND $this->_cart_contents[$items['rowid']]['duration'] == $items['duration'])

Where it says

Code:
$this->_cart_contents[$items['rowid']]['qty'] = $items['qty'];

I added

Code:
$this->_cart_contents[$items['rowid']]['duration'] = $items['duration'];

I think that's it.
#7

[eluser]packetfox[/eluser]
Excellent, thanks a lot. Ill need to revisit this, but for now it allowed me to process an extra field. Once i found a nice and solid way to process the options array i will share it here.

best regards,
DS
#8

[eluser]The Questioner[/eluser]
I've been working on my own ecommerce site based upon the cart class. Along the way, I've extended the cart class for some much-needed functionality. Here are the most useful functions (all to be placed in the cart class):-

To check if a cart item exists (by row ID):
Code:
function does_cart_item_exist($strRowID)
    //check if cart item exists by checking key
    {    
        return array_key_exists($strRowID, $this->_cart_contents);
    }

To retrieve a specific cart product item (by row ID):
Code:
function get_cart_item($strRowID)
    //retrieve specific cart item object array
    {
        //check if cart item exists first
        if (array_key_exists($strRowID,$this->_cart_contents))
        {
            return $this->_cart_contents[$strRowID];
        }
        else
        {
            return NULL;
        };
    }

To replace the contents of a cart product item with updated values (by row ID):
Code:
function update_existing_cart_item($strRowID, $arrData)
    //update specific cart item object array
    {
        $this->_cart_contents[$strRowID]=$arrData;    
        $this->_save_cart();
    }

$arrData has to contain the full array that is used when inserting a new cart item, as well as the rowid. For example:

Code:
//prepare all details for updated cart item
$data = array(
       'id'      => $objProduct->id,
       'qty'     => $objProduct->qty,
       'price'   => $objProduct->price_inc_vat,
       'name'    => $objProduct->extended_name,
       'options' => $arrProductAttributes
    );

//add rowid with existing cart item row id so that it knows what to replace
$data['rowid']=$strCartItemKey;
$this->cart->update_existing_cart_item($strCartItemKey,$data);

I hope this helps my fellow CodeIgnities.




Theme © iAndrew 2016 - Forum software by © MyBB