A customer has the option of selecting an outlet when they add an item to the cart. Even though a product may be associated with multiple outlets, the product only has 1 ID. If the user was to add the same product to the cart, but choose 3 separate branches, it would just just 1 line in the cart with a quantity of 3. That was not how it should work so I concatenated my ID with the outlet ID so that in the cart 3 separate items would appear even though it was the same product. This works fine but my problem comes in when there is not enough stock.
Let's say there is only 1 item in stock. If I add it to the cart and select a particular outlet, it adds to the cart. If you do this again and select another branch it should not add to the cart because there is only one on hand. It is however letting me add 1 for every different branch selected. It should only let me add 1 no matter how many different outlets I may try to choose.
$key = "{$prod_id}.{$outlet_id}"; if (empty($_SESSION['cart_array'][$key])) { $_SESSION['cart_array'][$key] = array( 'prod_id' => $prod_id, 'outlet_id' => $outlet_id, 'quantity' => $quantity, 'prod_name' => $data['prod_name'], 'sale_price' => $data['sale_price'], 'sp_name' => $data['sp_name'], 'outlet_name' => $data['outlet_name'] ); $response['success'] = true; $response['message'] = 'Product added to cart.'; } elseif($_SESSION['cart_array'][$key]['quantity'] >= $data['numVouchersLeft']) { $response['success'] = false; $response['message'] = 'Insufficient stock. Cannot add to cart.'; } else { $_SESSION['cart_array'][$key]['quantity'] += $quantity; $response['success'] = true; $response['message'] = 'Product added to cart.'; }
This is perhaps the line that needs to change?
elseif($_SESSION['cart_array'][$key]['quantity'] >= $data['numVouchersLeft']) {
UPDATE:
I tried this but for some reason if I add the same product from 2 different outlets, the quantity goes back to 1 for the $_SESSION['cart_array_products]
$key = "{$prod_id}.{$outlet_id}"; $keytwo = $prod_id; if (empty($_SESSION['cart_array'][$key])) { $_SESSION['cart_array'][$key] = array( 'prod_id' => $prod_id, 'outlet_id' => $outlet_id, 'quantity' => $quantity, 'prod_name' => $data['prod_name'], 'sale_price' => $data['sale_price'], 'sp_name' => $data['sp_name'], 'outlet_name' => $data['outlet_name'] ); $_SESSION['cart_array_products'][$keytwo] = array( 'prod_id' => $prod_id, 'quantity2' => $quantity ); $response['success'] = true; $response['message'] = 'Product added to cart.'; } elseif($_SESSION['cart_array'][$key]['quantity'] >= $data['numVouchersLeft'] || $_SESSION['cart_array_products'][$keytwo]['quantity2'] >= $data['numVouchersLeft']) { $response['success'] = false; $response['message'] = 'Insufficient stock. Cannot add to cart.'; } else { $_SESSION['cart_array'][$key]['quantity'] += $quantity; $_SESSION['cart_array_products'][$keytwo]['quantity2'] += $quantity; $response['success'] = true; $response['message'] = 'Product added to cart.'; }
3 Answers
Answers 1
Going off of your solution's code, I just simplified it so it only checks once if $keytwo quantity >= $data['numVouchersLeft']
.
if(empty($_SESSION['cart_array'][$key])) { $_SESSION['cart_array'][$key] = array( 'prod_id' => $prod_id, 'outlet_id' => $outlet_id, 'quantity' => $quantity, 'prod_name' => $data['prod_name'], 'sale_price' => $data['sale_price'], 'sp_name' => $data['sp_name'], 'outlet_name' => $data['outlet_name'] ); } else { $_SESSION['cart_array'][$key]['quantity'] += $quantity; } if (empty($_SESSION['cart_array_products'][$keytwo])){ $_SESSION['cart_array_products'][$keytwo] = array( 'prod_id' => $prod_id, 'quantity' => $quantity ); } else { $_SESSION['cart_array_products'][$keytwo]['quantity'] += $quantity; } if($_SESSION['cart_array_products'][$keytwo]['quantity'] >= $data['numVouchersLeft']) { $response['success'] = false; $response['message'] = 'Not enough stock to add item to cart.'; } else { $response['success'] = true; $response['message'] = 'Product added to cart.'; }
It may be worth renaming $keytwo
to something more intuitive: $simpleKey
or $productKey
? But then it may also be worth looking at what we could call $key
as well.
Hope this helps!
Answers 2
I managed to solve it like this but if someone has a better or more elegant way then please let me know:
if(empty($_SESSION['cart_array'][$key])) { $_SESSION['cart_array'][$key] = array( 'prod_id' => $prod_id, 'outlet_id' => $outlet_id, 'quantity' => $quantity, 'prod_name' => $data['prod_name'], 'sale_price' => $data['sale_price'], 'sp_name' => $data['sp_name'], 'outlet_name' => $data['outlet_name'] ); } elseif($_SESSION['cart_array_products'][$keytwo]['quantity'] < $data['numVouchersLeft']) { $_SESSION['cart_array'][$key]['quantity'] += $quantity; $response['success'] = true; $response['message'] = 'Product added to cart.'; } else { $response['success'] = false; $response['message'] = 'Not enough stock to add item to cart.'; } if(empty($_SESSION['cart_array_products'][$keytwo])) { $_SESSION['cart_array_products'][$keytwo] = array( 'prod_id' => $prod_id, 'quantity' => $quantity ); $response['success'] = true; $response['message'] = 'Product added to cart.'; } elseif($_SESSION['cart_array_products'][$keytwo]['quantity'] >= $data['numVouchersLeft']) { $response['success'] = false; $response['message'] = 'Not enough stock to add item to cart.'; } else { $_SESSION['cart_array_products'][$keytwo]['quantity'] += $quantity; $response['success'] = true; $response['message'] = 'Product added to cart.'; }
Answers 3
I believe the code inside the if ( empty( $_SESSION['cart_array'][ $key ] ) )
should be changed to this:
if ( empty( $_SESSION['cart_array'][ $key ] ) ) { $array_column = array_column( $_SESSION['cart_array'], 'quantity', 'prod_id' ); if ( array_key_exists( $prod_id, $array_column ) ) { // product is already inside the cart so check quantity if ( $array_column[ $prod_id ] >= $data['numVouchersLeft'] ) { // Insufficient stock $response['success'] = false; $response['message'] = 'Insufficient stock. Cannot add to cart.'; } else { // product has some stock so add it $_SESSION['cart_array'][ $key ] = array( 'prod_id' => $prod_id, 'outlet_id' => $outlet_id, 'quantity' => $quantity, 'prod_name' => $data['prod_name'], 'sale_price' => $data['sale_price'], 'sp_name' => $data['sp_name'], 'outlet_name' => $data['outlet_name'] ); $response['success'] = true; $response['message'] = 'Product added to cart.'; } } else { // product is not in the cart so add it $_SESSION['cart_array'][ $key ] = array( 'prod_id' => $prod_id, 'outlet_id' => $outlet_id, 'quantity' => $quantity, 'prod_name' => $data['prod_name'], 'sale_price' => $data['sale_price'], 'sp_name' => $data['sp_name'], 'outlet_name' => $data['outlet_name'] ); $response['success'] = true; $response['message'] = 'Product added to cart.'; } }
I believe that when you add an item and check the relationship key you end up inside this if statement if ( empty( $_SESSION['cart_array'][ $key ] ) )
even if the item is present. That is a functionality that you wanted to keep, so all you have to do is check the stock at that specific point. First I check if the item product is present inside the cart. If not you simply continue and add the item. however, if it is you need to check the stock. By this point the $array_column
should look something like this:
Array ( [{product id}] => { quantity } [{product id}] => { quantity } [{product id}] => { quantity } etc. )
By using this data you can check the $array_column[ $prod_id ]
(we know that $pro_id
is inside this $array_column
as a key because of our previous check)
Finally, if this returns true you have "Insufficient stock. Cannot add to cart."
I believe that should be enough information about my thought process. There shouldn't be a reason to change anything else.
NOTE: I made this looking at your first code you shared, not your updated code.
0 comments:
Post a Comment