Magento – Add to cart issue – Magento 2.1

addtocartajaxmagento-2.1magento2

In Magento 2.1 When I enable Ajax on add to cart on Product detailed Page by "bindSubmit": true in add-to-cart.phtml

<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "catalogAddToCart": {
                "bindSubmit": false
            }
        }
    }
</script>

to

<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "catalogAddToCart": {
                "bindSubmit": true
            }
        }
    }
</script>

Now after clearing cache, I added a simple product to cart which was perfect, but on product with Option(Configurable option) when I add to cart without selecting the option, It show error "This is a required field.", but the ajax starts already and refresh the page giving error "Please specify product's required option(s).". Meaning Ajax add to cart don't wait for me to select the option, when I do not select option.

Hope you understand, what I am trying to say.

Best Answer

Should add more validation:

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

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

See more here.

Related Topic