Welcome Guest, Not a member yet? Register   Sign In
Help with userdata and array.
#1

[eluser]LeMec[/eluser]
Hello all,

I have to modify a functionality of an existing product build using Codeigniter.

The system adds items to a list of Items until the transaction is completed.
As it is now, the system looks if the item already exists and adds one to the quantity.

It does not add a new item to the list if it is already there.
I want to change that so that it adds the item to the list even if it already exists.

Here is the relevant portion of code:

function add_item($item_id,$quantity=1,$discount=0,$price=null,$tax=null)
{
//make sure item exists
if(!$this->CI->Item->exists($item_id))
{
//try to get item id given an item_number
$item_id = $this->CI->Item->get_item_id($item_id);

if(!$item_id)
return false;
}

$items = $this->get_cart();
$item = array($item_id=>
array(
'name'=>$this->CI->Item->get_info($item_id)->name,
'quantity'=>$quantity,
'discount'=>$discount,
'price'=>$price!=null ? $price: $this->CI->Item->get_info($item_id)->unit_price
)
);

//Item already exists, add to quantity
if(isset($items[$item_id]))
{
$items[$item_id]['quantity']+=$quantity;
}
else
{
//add to existing array
$items+=$item;
}

$this->set_cart($items);
return true;

}

I tried to modify the condition and simply execute the:

$items+=$item;

statement unconditionaly, but it does not work.

Here is the Get_Cart Set_Cart routines.

function get_cart()
{
if(!$this->CI->session->userdata('cart'))
$this->set_cart(array());

return $this->CI->session->userdata('cart');
}

function set_cart($cart_data)
{
$this->CI->session->set_userdata('cart',$cart_data);
}


I am a newbee. Please let me know if you need more information.

Many thanks.
#2

[eluser]WanWizard[/eluser]
That's from a functionality point of view a bit odd.

Normally, when an item is already present in the cart, you don't add it again, you increase the quantity of the item already in the cart.

Don't you think it is going to be confusing for the users if items appear multiple times?
#3

[eluser]LeMec[/eluser]
Thanks for your reply.

No, I do not think It will be confusing. This is actually a request from someone.
Some comments and unique serial number have to be added for each Item. So I can not
have the quantity increase.

Please advise.

Alain




Theme © iAndrew 2016 - Forum software by © MyBB