In Prestashop, I created a custom form in which I display a list with all the products, and the user can fill in the appropriate quantities. By submitting the form, I empty the basket and fill it with new values and finally redirect to the verification page.
Everything works fine, but only when the basket already exists. In the case of an empty basket (cart_id == null) I can not add products. I tried several ways to create a $ basket, but I failed.
I do not get any exceptions, the code runs without errors, it is just that in the end, on the check page, the basket remains empty; I repeat, only when the cart was already empty. In the case of a cart with products in it, the process is just perfect!
I would appreciate help on this.
Here is my little controller that receives products and quantities from the form and adds them to the cart:
include('../../config/config.inc.php');
include('../../header.php');
$products = array();
foreach ($_POST as $key => $value)
if (substr($key, 0, 9) === 'quantity_')
$products[substr($key, 9)] = $value;
$prods = $cart->getProducts();
foreach ($prods as $prod)
$cart->deleteProduct($prod['id_product']);
foreach ($products as $product_id => $quantity)
if ($quantity > 0)
$cart->updateQty($quantity, $product_id);
header("Location: " . $_POST['redirect']);
exit();`
Thank you in advance!
source
share