Jump to content

[PARTIALLY SOLVED] PS 1.7 Add product to cart not working


philoupin

Recommended Posts

+++ EDIT September 2019 ++++

Don't apply the "fix" described below in PHP,  since it just fixes some symptoms (not all) and is not a solution to the source of the problem.

You probably have a javascript error somewhere, preventing correct execution of another script. Most probably this error is due to an incompatibility of a module/plugin.

+++ END EDIT +++

On our installation of PS 1.7.4.4 we got reports of some customers, that they could not add products to the cart. A click on the button "Add to cart" took the customer to the view of the cart contents which said "no items in cart" or something like this. The modal dialog "product added to cart" we ourselves usually see at this point with the option to continue shopping or go to checkout was not appearing. The customer was using Windows 10 with the "lastest version of Firefox" but we could only duplicate this faulty behaviour with Internet Explorer on Windows 10.

The option to "disable ajax" in the shopping cart module settings did not solve this problem.

Finally we discovered that in controllers/front/CartController.php around line 231 this condition was failing:

if (Tools::getIsset('add') || Tools::getIsset('update')) {
    $this->processChangeProductInCart();
}

On the faulty system the POST values did not contain the variables add=1 action=update which were being POSTed on a setup where adding to cart was working for us as expected. It seems that in themes/core.js around line 1753 these values are added via javascript (see last line):

$body.on('click', '[data-button-action="add-to-cart"]', function (event) {
  event.preventDefault();
  if ((0, _jquery2['default'])('#quantity_wanted').val() > (0, _jquery2['default'])('[data-stock]').data('stock') && (0, _jquery2['default'])('[data-allow-oosp]').data('allow-oosp').length === 0) {
    (0, _jquery2['default'])('[data-button-action="add-to-cart"]').attr('disabled', 'disabled');
  } else {
    var _ret = (function () {
      var $form = (0, _jquery2['default'])(event.target).closest('form');
      var query = $form.serialize() + '&add=1&action=update';

Our solution consisted in modifying /themes/YOURTHEME/templates/catalog/product.tpl to add two hidden fields with name="add" and name="action" around line 101:

<form action="{$urls.pages.cart}" method="post" id="add-to-cart-or-refresh">
  <input type="hidden" name="token" value="{$static_token}">
    <input type="hidden" name="add" value="1">
    <input type="hidden" name="action" value="update">
  <input type="hidden" name="id_product" value="{$product.id}" id="product_page_product_id">
  <input type="hidden" name="id_customization" value="{$product.id_customization}" id="product_customization_id">

In our previously functioning environment, these values are now posted twice, resulting in "add" and "action" becoming arrays, but it still seems to be working. These are the POSTed values

token	93865fd2ed163bfa72111fcbe7a39xyz
add	[…]
0	1
1	1
action	[…]
0	update
1	update
id_product	4
id_customization	0
group[1]	25
group[5]	31
qty	1

Although we have resolved our immediate problem for now, we would prefer to understand why the add-to-cart-button doesn't degrade gracefully to still function on an older browser or system. As I stated above our customer was himself on the "current version" of Firefox (we were unfortunately not able to confirm the version number), so it definitely is not just a a problem of very old browsers.

Any hints would be appreciated and if nothing else, maybe this will help someone stumbling upon the same issue.

 

 

 

Edited by philoupin
Clarification of issue / solution (see edit history)
Link to comment
Share on other sites

Possibly part of the same problem we get reposts that the final "order" button isn't working for these same customers. There too the POSTing is done via Javascript. 

In my opinion the "add to cart" and "place order" buttons are among the most important buttons in the shop and should be as compatible as possible also with older browsers.

I am trying to gather more info about the customer PC system on which the error occurs, we can currently only reproduce it on Internet Explorer on Windows 10. Not the most modern setup, however, customers who can't order are always right 🙂

 

Any help or pointers would be much appreciated!

P.

 

Link to comment
Share on other sites

  • 8 months later...

I made the change you put in your original post - It now adds to cart on Internet Explorer, but only on the product page (not on the homepage "quick add to cart" button).

 

I also noticed it doesn't let you modify the amount ordered in the checkout on IE. And on all the other browsers, if you choose a quantity of 2, it doubles to 4.

 

:( This is really disappointing. I need this fixed or I am going to give up with PrestaShop. It has been nothing but trouble since installing it. Cannot even update to the latest version without it breaking my store. Really annoying me now and I am sick of the constant problems.

Link to comment
Share on other sites

Hi Sampsonzak,

I would not recommend that you use the fix I posted above, as it did not solve the source of the problem, just fix some symptoms. If I remember well, the source of the problem was in a javascript not loading in IE. For us this javascript was part of the Newsletter Pro plugin. i suggest you look at the javascript console of Internet Explorer, there it may tell you what javascript is causing problems, if this is in fact the same problem you are having.

Best regards,

p.

Link to comment
Share on other sites

4 minutes ago, philoupin said:

Hi Sampsonzak,

I would not recommend that you use the fix I posted above, as it did not solve the source of the problem, just fix some symptoms. If I remember well, the source of the problem was in a javascript not loading in IE. For us this javascript was part of the Newsletter Pro plugin. i suggest you look at the javascript console of Internet Explorer, there it may tell you what javascript is causing problems, if this is in fact the same problem you are having.

Best regards,

p.

Hi, thanks for the reply.

 

I am really clueless about errors with javascript - I only know basic PHP/HTML/CSS. No idea about Javascript or jQuery though.

 

The "error" IE is showing in it's console is a "syntax error", which points to this line of JavaScript code RE the cart:

> prestashop.on('updateCart',(params)=>{if(typeof(params)!=='undefined'&&typeof(prestashop.cart)!=='undefined'){var iso_code=prestashop.currency.iso_code,products=prestashop.cart.products,my_id=params.reason.idProduct,

 

Does anything in that maybe stick out to you as incorrect syntax?

 

Really appreciate if you could help. I only have the default modules installed: Newsletter Block / and a Newsletter Block which came with my theme also ?

Link to comment
Share on other sites

Wow ! Never mind, I have found the problem!!

 

If anyone has this issue, my issue is caused by the 'Official Facebook Pixel' plugin by PrestaShop. Wow!!

 

Works flawlessly on Internet Explorer now. Now I need to find a fix for this Pixel plugin. Did not expect that to be the issue!!

 

Thank you OP for making me look into the javascript error a bit more. Thank you ! :D

Link to comment
Share on other sites

Did you learn any javascript debugging? I am clueless on how to do this.

We have bug that need javascript debugging/comparison:

Changing product quantity on check-out page on 1.6.1.4 v is causing bug in our webpage. The strange thing is that if we continue to payment and then go back to checkout it is possible to change product quantity and the bug does not occur.

Link to comment
Share on other sites

6 minutes ago, MacMaster said:

Did you learn any javascript debugging? I am clueless on how to do this.

We have bug that need javascript debugging/comparison:

Changing product quantity on check-out page on 1.6.1.4 v is causing bug in our webpage. The strange thing is that if we continue to payment and then go back to checkout it is possible to change product quantity and the bug does not occur.

I just inspected element in microsoft and I saw the issue was pointing to a bracket - I followed the bracket until the end and then I noticed javascript relating to a plugin was at the end of it, disabled the plugin and it worked flawlessly.

 

If you send me your link and the steps I will take a look and see if I can find anything by the same way I checked mine out if you'd like

Link to comment
Share on other sites

I managed to resolve the issue. I uninstalled the "Advanced One Page Checkout". Installed the  "One Page Checkout for Prestashop v2.3.2" with error. Uninstalled it again and reinstalled the  "Advanced One Page Checkout" again. Suddenly it worked correctly.

(It seems some table or stuff was missing in the override, because just uninstalling and reinstall the "Advanced One Page Checkout" did not fix it. It required temporary installation of other checkout to reset the issue. )

Link to comment
Share on other sites

  • 1 year later...

I have similar problem. When i try to add product to cart i recieve this error:

core.js:116 Uncaught TypeError: Cannot read property 'length' of undefined
    at HTMLButtonElement.<anonymous> (core.js:116)
    at HTMLBodyElement.dispatch (core.js:39)
    at HTMLBodyElement.m.handle (core.js:39)

It refers to:

("[data-allow-oosp]").data("allow-oosp").length)

 

It does not happen in all products 

Link to comment
Share on other sites

  • 2 months later...

If somebody get this error again, we had to fix it modifying 2 files:

/themes/core.js, replace "&add=1&action=update" by "&add=" +(0, r.default)("#quantity_wanted").val() "&action=update"

 

and then, in the Cart Controller, in the processChangeProductInCart, add the following:

if ($mode == 'add' && Tools::getValue('add')) {
      $this->qty = (int)Tools::getValue('add');
}

after the first line -> $mode = (Tools::getIsset('update') && $this->id_product) ? 'update' : 'add';

Hope it helps somebody. By the way, I dont know if this behavious is only in some prestashop versions or what, but it seems Prestashop wanted to force to add only 1 by one, not allowing to add bigger quantities,

Regards

Link to comment
Share on other sites

  • 1 month later...
On 9/13/2021 at 11:00 PM, pelleringa said:

If somebody get this error again, we had to fix it modifying 2 files:

/themes/core.js, replace "&add=1&action=update" by "&add=" +(0, r.default)("#quantity_wanted").val() "&action=update"

 

and then, in the Cart Controller, in the processChangeProductInCart, add the following:

if ($mode == 'add' && Tools::getValue('add')) {
      $this->qty = (int)Tools::getValue('add');
}

after the first line -> $mode = (Tools::getIsset('update') && $this->id_product) ? 'update' : 'add';

Hope it helps somebody. By the way, I dont know if this behavious is only in some prestashop versions or what, but it seems Prestashop wanted to force to add only 1 by one, not allowing to add bigger quantities,

Regards

I think you forgot the + sign

 

/themes/core.js, replace "&add=1&action=update" by "&add=" + (0, r.default)("#quantity_wanted").val() + "&action=update"

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...