6

I have check online demo of Magento 2
http://magento2-demo.nexcess.net/fusion-backpack.html
when we click on add to cart it add product via AJAX but i have Set up demo on localhost it don't to add Product to cart via AJAX.

Can anyone tell where is settings to enable ajax on add to cart ?

9
  • have you seen any error in console? Jul 14, 2016 at 9:16
  • It will redirect to shopping cart page or others? Jul 14, 2016 at 9:50
  • no error in console Jul 14, 2016 at 10:05
  • it don't redirect to cart page but refresh on same page Jul 14, 2016 at 10:10
  • Maybe it's because you are with blank theme. Try to switch to Luma and you will get AJAX add to cart. Jul 14, 2016 at 11:04

2 Answers 2

32

You have to just override addtocart.phtml file

app/design/frontend/Packagename/themename/Magento_Catalog/templates/product/view/addtocart.phtml

Replace "bindSubmit": false to "bindSubmit": true

<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>

Remove cache and check its in detail page.

9
  • That was quick and easy. One thing though I have Changed Button text From "Add to Cart" to "Add to Bag" But when it is added to cart the Text Revert Back to "Add to Cart" Where to change it. Aug 9, 2016 at 9:16
  • you can try to change name of add to cart from here vendor/magento/module-catalog/i18n/en_US.csv with replace in second column add to cart to add to bag Aug 9, 2016 at 9:22
  • How/ where do i override the file vendor/magento/module-catalog/i18n/en_US.csv Aug 9, 2016 at 9:32
  • in your app/design/frontend/Vendorname/themename/i18n folder Aug 9, 2016 at 9:34
  • 1
    Hello @Rakesh, I have similar issue and got solution from your answer. But in case of Ver 2.1.2. after ajax add to cart page is getting refreshed. You have any solution to prevent the page loading in Magento 2.1.2 ? Dec 7, 2016 at 11:00
0

Add method to your class

public function __construct(
    ......................................................
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Catalog\Block\Product\ListProduct $listProductBlock,
    ......................................................
) {
    ......................................................
    $this->productFactory = $productFactory;
    $this->listProductBlock = $listProductBlock;
    ......................................................
}

public function getProductData($id)
{
    $collection = $this->productFactory->create()->load($id);
    return $collection;
}

public function getAddToCartPostParams($product)
{
    return $this->listProductBlock->getAddToCartPostParams($product);
}

Add into your phtml file

<?php
$_product = $block->getProductData(11);
?>
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<form data-role="tocart-form" action="<?php /* @escapeNotVerified */ echo $postParams['action']; ?>" method="post">
    <input type="hidden" name="product" value="<?php /* @escapeNotVerified */ echo $postParams['data']['product']; ?>">
    <input type="hidden" name="<?php /* @escapeNotVerified */ echo "uenc"; ?>" value="<?php /* @escapeNotVerified */ echo $postParams['data']['uenc']; ?>">
    <?php echo $block->getBlockHtml('formkey')?>
    <button type="submit"
            title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
            class="action tocart primary">
        <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
    </button>
</form>
<script type="text/x-magento-init">
    {
        "[data-role=tocart-form]": {
            "Magento_Catalog/js/validate-product": {
                "bindSubmit": true
            }
        }
    }
</script>
1
  • why productFactory + load and not productRepository->getById()? Also in here $collection = $this->productFactory->create()->load($id); $id is undefined
    – Marius
    Sep 21, 2021 at 9:54

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.