Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CE 2.1] Ajax cart doesn't work #5144

Closed
minhluan259 opened this issue Jun 21, 2016 · 13 comments
Closed

[CE 2.1] Ajax cart doesn't work #5144

minhluan259 opened this issue Jun 21, 2016 · 13 comments
Labels
bug report Component: Catalog Component: Checkout Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development

Comments

@minhluan259
Copy link

minhluan259 commented Jun 21, 2016

I installed CE 2.1 with sample data.
In product page, home page, top category (landing page) : Ajax cart doesn't work.
Ajax cart only work in sub category.

@pboisvert
Copy link

@asemenenko Can you make sure this one gets tested/verified?

@minhluan259
Copy link
Author

Today CE 2.1 release, and it have the same issue.

@minhluan259 minhluan259 changed the title [CE 2.1 RC3] Ajax cart don't work [CE 2.1] Ajax cart doesn't work Jun 24, 2016
@stevekem
Copy link

stevekem commented Jun 24, 2016

Having same issue with 2.1 CE

@minhluan259
Copy link
Author

minhluan259 commented Jun 25, 2016

@asemenenko @pboisvert @stevekem , I found the issue in file:
vendor\magento\module-catalog\view\frontend\templates\product\view\addtocart.phtml

Here the fix:

<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/product/view/validation": {
                "radioCheckboxClosest": ".nested"
            }
        }
    }
</script>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "catalogAddToCart": {
                "bindSubmit": false
            }
        }
    }
</script>
<?php endif; ?>

Change to

<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script>
    require([
        'jquery',
        'mage/mage',
        'Magento_Catalog/product/view/validation',
        'Magento_Catalog/js/catalog-add-to-cart'
    ], function ($) {
        'use strict';

        $('#product_addtocart_form').mage('validation', {
            radioCheckboxClosest: '.nested',
            submitHandler: function (form) {
                var widget = $(form).catalogAddToCart({
                    bindSubmit: false
                });

                widget.catalogAddToCart('submitForm', $(form));

                return false;
            }
        });
    });
</script>
<?php else : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/product/view/validation": {
                "radioCheckboxClosest": ".nested"
            }
        }
    }
</script>
<?php endif; ?>

@EugeneAinstainer
Copy link

I have the same issue with magento 2.1. @minhluan259 thanks for fix, but does somebody know about the status of this issue?

@Zaylril
Copy link

Zaylril commented Jul 4, 2016

@EugeneAinstainer @asemenenko @minhluan259

vendor\magento\module-catalog\view\frontend\templates\product\view\addtocart.phtml

You simply need to change

"bindSubmit": false

to

"bindSubmit": true

The initial value in the old code was overridden by the code in the inline script. However if you actually look at the widget itself you will see nothing happens if bind is set to false. NOTHING it's like the widget doesn't exist. How this was missed is just beyond comprehension. Unbelievable

Hopefully the above fix is a much better one than simply adding in the old code.

Cheers

@minhluan259
Copy link
Author

@Zaylril
"bindSubmit": true
only work for simple product (hasn't option).
If product has an option required, it need validate before send request ajax.

@Zaylril
Copy link

Zaylril commented Jul 4, 2016

@minhluan259 yes you are correct because the validation is not called. So this needs to be factored into the addToCart Widget.

So a quick 2 liner update to the widget, need to modify the bindSubmit function

   _bindSubmit: function() {
            var self = this;
           this.element.mage('validation');
            this.element.on('submit', function(e) {
                e.preventDefault();
                if(self.element.valid()) {
                    self.submitForm($(this));
                }
            });
        }

@kfaer
Copy link

kfaer commented Jul 10, 2016

It is bug sirs, there is an Ajax issue in M2.1, when adding an item to the cart "Adding.." sign is not showing in the box of add to cart

@davidbilly
Copy link

davidbilly commented Jul 27, 2016

@Zaylril Hi, May i know where this _bindSubmit: function() { code locate? Thanks :D

Never mind, i found it : ./vendor/magento/module-catalog/view/frontend/web/js/catalog-add-to-cart.js

I edited : 
    {
        "#product_addtocart_form": {
            "catalogAddToCart": {
                "bindSubmit": true
            }
        }
    }

at ./vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml 

And 

_bindSubmit: function() {
            var self = this;
        this.element.mage('validation');
            this.element.on('submit', function(e) {
                e.preventDefault();
        if(self.element.valid()) {
                   self.submitForm($(this));
        }
            });
        },

at ./vendor/magento/module-catalog/view/frontend/web/js/catalog-add-to-cart.js 

After that, do bin/magento setup:static-content:deploy and clean web browser cache.
The mini cart still show empty :(

Please help on this #5426 (comment) :(

Update : Mini cart empty issue fixed with #5003

@minhluan259
Copy link
Author

The issue was fixed, Why has it still not been updated in magento 2.1.4?

@oleg-onufer
Copy link
Contributor

Hi @minhluan259.
Thanks for reporting this issue.
Internal ticket MAGETWO-70477 was created.

@veloraven veloraven added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Jul 6, 2017
@magento-engcom-team magento-engcom-team added 2.1.x Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development bug report Component: Catalog Component: Checkout labels Sep 11, 2017
@magento-engcom-team magento-engcom-team added the Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed label Sep 11, 2017
@magento-engcom-team magento-engcom-team added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch labels Oct 18, 2017
@magento-engcom-team
Copy link
Contributor

@minhluan259, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.
We tested the issue on 2.3.0-dev, 2.2.0, 2.1.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Component: Catalog Component: Checkout Issue: Cannot Reproduce Cannot reproduce the issue on the latest `2.4-develop` branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Format is not valid Gate 1 Failed. Automatic verification of issue format is failed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development
Projects
None yet
Development

No branches or pull requests