Hello,
I've been developing a module to add a "Daily Limit" to customer purchase and basically prevent them from completely adding any product in their shopping cart if it surpass that daily limit.
It all went mostly smoothly, except 2 problem I encountered,
- How do I prevent the item to be added to the shopping card from the "actionCartUpdateQuantityBefore" hook.
- Why is one of my sites still let customer add an item to their shopping cart but another site does not, even tho both of these sites have the same Stock management setting of not allowing backorder.
I will briefly explain what I did for this module:
What I did was, add this hook "actionCartUpdateQuantityBefore" to the module,
then check where this hook is called, it's from Cart.php, checking the parameters and some code, the important parts is this:
$data = array( 'cart' => $this, 'product' => $product, 'id_product_attribute' => $id_product_attribute, 'id_customization' => $id_customization, 'quantity' => $quantity, 'operator' => $operator, 'id_address_delivery' => $id_address_delivery, 'shop' => $shop, 'auto_add_cart_rule' => $auto_add_cart_rule, ); /* @deprecated deprecated since 1.6.1.1 */ // Hook::exec('actionBeforeCartUpdateQty', $data); Hook::exec('actionCartUpdateQuantityBefore', $data); if ((int) $quantity <= 0) { return $this->deleteProduct($id_product, $id_product_attribute, (int) $id_customization); } if (!$product->available_for_order || ( Configuration::isCatalogMode() && !defined('_PS_ADMIN_DIR_') ) ) { return false; }
As you can see from the code above, I get the $data and I used them in my module to check if the user surpassed the daily limit or not.
if the "price of his purchase today" + "price of the shopping card" + "price of the product" he is trying to add, is not above the daily limit.
Do nothing, otherwise, some new data will be shown using the "displayCartModalContent" to tell them of the daily limit which works fine.
Now we reach the first problem:
- How do I prevent the item to be added to the shopping card from the "actionCartUpdateQuantityBefore" hook.
To solve this above problem, I did a simple trick,
I simply write
$data['product']->available_for_order = false;
once this data is changed in my module hook, then the Cart will be returned false.
due to this:
if (!$product->available_for_order || ( Configuration::isCatalogMode() && !defined('_PS_ADMIN_DIR_') ) ) { return false; }
which should work fine, and it indeed worked fine in localhost and my first site, Prestashop Version 1.7.6.4.
But then I tried to install this in my 2nd site, which a few years ago was the clone of the first site, but changed a bit over time, and it's Version 1.7.6.5 right now.
in this site, while both the "action" and "display" hook from my module works perfectly,
The customer can still add the item to it's shopping cart...
They see my daily limit error from my display hook showing up and telling them they are over the limit.
But their shopping cart still add the product regardless of that.
To completely make sure this is not my module's problem,
I created a product out of stock, then using a customer account, tried to add it to cart, the button is "disabled"
but a simple right click and Inspect Elements, removing the "disabled" attribute from the button and you can click it.
Then it will actually add the product to my shopping cart! this is a problem.
Same trick does not work in first site, it give an error and does not add the product.
Both site seems to have similar settings and configuration, the only difference is Version 1.7.6.4 on first and Version 1.7.6.5 on second site.
Any Idea ?
Also, is there another way I could use to prevent a product from being added to the shopping cart from the "actionCartUpdateQuantityBefore" hook ?
Thanks in advance.
Edit: I might as well add that I rather do this simply from my module and without touching any of Prestashop core files or overriding anything.