Daniel West
Daniel West

Reputation: 1808

Codeigniter - Add a product with the same ID if options are different

I'm currently building a simple ecommerce site and have ran into an interesting problem.

Basically, the products are in the products table however there are also a number of other options such as price, colour etc... some of these have an effect on the total price.

However, if the same product but with a different set of options exist then the product id is obviously the same and the item is not added into the codeigniter cart.

The easiest way that I have thought of is to allow products with the same id to be included in the cart, is this easily possible? Is there a better way of tackling this problem?

Any ideas will be very helpful!

Upvotes: 3

Views: 3163

Answers (1)

Daniel West
Daniel West

Reputation: 1808

OK so I found an answer to my problem which people may find helpful.

Basically I was using multidimensional arrays for the options which is not supported by Codeigniter's cart class.

This was then generating the same row id due to the fact that the arrays were showing up as array but may also have been breaking the implode function used to generate the hash in the Codeigniter Cart class.

To fix this you can simple replace this line within the CI_Cart class:

$rowid = md5($items['id'].implode('', $items['options']));

with this line:

$rowid = md5($items['id'].serialize($items['options']));

to fix the problem.

This then creates a storable representation of the multidimensional array as text that is then hashed and will always be unique if there is a multidimensional array used as the options value.

Upvotes: 7

Related Questions