Welcome Guest, Not a member yet? Register   Sign In
A question about CI cart
#1

[eluser]China-cier[/eluser]
Dear all.
today i want to user ci cart library.
when a add a product in my cart,i want to get the row id as a return value.

that row id's description:
What is a Row ID? The row ID is a unique identifier that is generated by the cart code when an item is added to the cart. The reason a unique ID is created is so that identical products with different options can be managed by the cart.

For example, let's say someone buys two identical t-shirts (same product ID), but in different sizes. The product ID (and other attributes) will be identical for both sizes because it's the same shirt. The only difference will be the size. The cart must therefore have a means of identifying this difference so that the two sizes of shirts can be managed independently. It does so by creating a unique "row ID" based on the product ID and any options associated with it.

In nearly all cases, updating the cart will be something the user does via the "view cart" page, so as a developer, it is unlikely that you will ever have to concern yourself with the "row ID", other then making sure your "view cart" page contains this information in a hidden form field, and making sure it gets passed to the update function when the update form is submitted. Please examine the construction of the "view cart" page above for more information.

thanks
#2

[eluser]theprodigy[/eluser]
Quote:when a add a product in my cart,i want to get the row id as a return value.
This would be rather difficult to do considering CI's Cart class allows you to insert multiple items with a single call.
Quote:By using a multi-dimensional array, as shown below, it is possible to add multiple products to the cart in one action. This is useful in cases where you wish to allow people to select from among several items on the same page.
Code:
$data = array(
               array(
                       'id'      => 'sku_123ABC',
                       'qty'     => 1,
                       'price'   => 39.95,
                       'name'    => 'T-Shirt',
                       'options' => array('Size' => 'L', 'Color' => 'Red')
                    ),
               array(
                       'id'      => 'sku_567ZYX',
                       'qty'     => 1,
                       'price'   => 9.95,
                       'name'    => 'Coffee Mug'
                    ),
               array(
                       'id'      => 'sku_965QRS',
                       'qty'     => 1,
                       'price'   => 29.95,
                       'name'    => 'Shot Glass'
                    )
            );

$this->cart->insert($data);
If you insert 3 items into the cart, which row id would you expect to get?
#3

[eluser]China-cier[/eluser]
thanks theprodigy
now my requirement just support a item.that is user just can add one product in cart at the same time.so i want to get this rowid,and record it for next page.

now i readed the cart source

=======================================================================
if (isset($items['options']) AND count($items['options']) > 0)
{
$rowid = md5($items['id'].implode('', $items['options']));
}
else
{
// No options were submitted so we simply MD5 the product ID.
// Technically, we don't need to MD5 the ID in this case, but it makes
// sense to standardize the format of array indexes for both conditions
$rowid = md5($items['id']);
}
=============================================================================
now i can get the row id by this algorithm.but i do not think it's elegant way.....

so i want to get more sound.
so do you understard?
#4

[eluser]theprodigy[/eluser]
I think the best way to handle that is to create a MY_Cart and extend CI_Cart. You don't want to change what the insert function returns. Right now it is return true/false. So, if you look at the file I have attached, you will see that I am setting a class level variable, and then created a public getter function to get the value of the variable. I haven't tried it yet, but this should at least give you a good starting point.
#5

[eluser]jdutoit[/eluser]
Hi I am having a serious problem that I'm sure has a simple solution.

I'm trying to add to the qty value in my cart not replace the qty value when I choose the same item, I am guessing it is overriding the item with the same unique rowid, here is my code (taken from a tutorial)


Controller

function add() {

$this->load->model('Products_model');

$product = $this->Products_model->get($this->input->post('id'));

$insert = array(
'id' => $this->input->post('id'),
'qty' => $this->input->post('qty'),
'price' => $product->price,
'name' => $product->name
);
if ($product->option_name) {
$insert['options'] = array(
$product->option_name => $product->option_values[$this->input->post($product->option_name)]
);
}

$this->cart->insert($insert);

redirect('shop');

}

Model

class Products_model extends Model {

function get_all() {

$results = $this->db->get('products')->result();

foreach ($results as &$result) {

if ($result->option_values) {
$result->option_values = explode(',',$result->option_values);
}

}

return $results;

}

function get($id) {

$results = $this->db->get_where('products', array('id' => $id))->result();
$result = $results[0];

if ($result->option_values) {
$result->option_values = explode(',',$result->option_values);
}

return $result;
}

}

Any suggestion would be greatly appreciated.

Thx,

Jean
#6

[eluser]WanWizard[/eluser]
If you insert a cart item, the internal item key is calculated using the item ID and the options array. If the calculated key already exists, it will be overwritten.

If you want to update the quantity, use the update() method. It still doesn't allow you to increment the quantity, you'll have to extend the Cart class and add an increment method (or modify the update method).
#7

[eluser]China-cier[/eluser]
Thx All
Make sense.
according to theprodigy's idea, now i've got the row id
Thx All
cool smile
#8

[eluser]jdutoit[/eluser]
Thanks for the advise WanWizard Ill give it a go.




Theme © iAndrew 2016 - Forum software by © MyBB