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
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()) ?>"
}
}
}
</script>
<?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