I have to add add to cart button on homepage. I have custom phtml in my own module. I would like this button to work with ajax. Could someone please suggest something?

1 Answer 1


Please add the below code at last in your phtml file and then check after cache flush.

<?php if (!$block->isRedirectToCartEnabled()) : ?>
    <script type="text/x-magento-init">
            "[data-role=tocart-form], .form.map.checkout": {
                "catalogAddToCart": {
                    "product_sku": "<?= $block->escapeJs($_product->getSku()) ?>"
<?php endif; ?>

Note: Make sure you have to set the No option from admin which steps i have given below.

Admin -> Stores -> Configuration -> Sales -> Checkout -> Shopping Cart -> After Adding a Product Redirect to Shopping Cart to No

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.