magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.56k stars 9.32k forks source link

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

Closed minhluan259 closed 7 years ago

minhluan259 commented 8 years ago

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 commented 8 years ago

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

minhluan259 commented 8 years ago

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

stevekem commented 8 years ago

Having same issue with 2.1 CE

minhluan259 commented 8 years ago

@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 commented 8 years ago

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

Zaylril commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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 commented 8 years ago

@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 https://github.com/magento/magento2/issues/5426#issuecomment-231778311 :(

Update : Mini cart empty issue fixed with #5003

minhluan259 commented 7 years ago

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

oleg-onufer commented 7 years ago

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

magento-engcom-team commented 7 years ago

@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