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.';
}
from Limit number of items added to cart based only on ID even though key has ID and concatenated value
No comments:
Post a Comment