Implementing AJAX Add to Cart Functionality in Magento
Achieving an AJAX-powered add-to-cart functionality with a popup box that displays “has been added to cart” message along with “continue shopping” and “proceed to checkout” buttons is quite straightforward in Magento, especially when using the VS-Ajax add to cart extension. Let’s walk through the steps required to implement this.
Understanding the VS-AJAX Add to Cart Extension
The VS-AJAX add to cart extension enhances the default Magento functionality by allowing products to be added to the cart without page refreshes using AJAX. This extension provides many features, including a customizable popup that can display after a successful addition to the cart.
Configuring the VS-AJAX Add to Cart Popup
To configure the popup box in your Magento store:
- Navigate to Stores > Configuration > Sales > VS-AJAX Add To Cart.
- Expand the AJAX Cart Pop-up section.
- Enable the Enable AJAX Cart Pop-up option.
Customizing the Popup Content
To display the desired message and buttons in your popup, you need to customize the template file located at:
app/design/frontend/Vendors/theme/Magento_VS_AjaxAddtoCart/templates/ajaxcart/popups.phtml
In this file, look for the following lines of code within the <div id="ajaxcart-popup-content"> container:
<div class="ajaxcart-popup-message"><?= $popupMessage ?></div>
<div class="ajaxcart-popup-buttons">
<button data-action="continue-shopping" type="button" class="action ajaxcart-continue-shopping"><?= $buttonContinueText ?></button>
<button data-action="checkout-cart" type="button" class="action ajaxcart-checkout-cart"><?= $buttonCheckoutText ?></button>
</div>
Here, you can modify the $popupMessage, $buttonContinueText, and $buttonCheckoutText variables to display your custom messages. For example:
$popupMessage = 'has been added to cart';
$buttonContinueText = 'Continue Shopping';
$buttonCheckoutText = 'Proceed to Checkout';
Ensuring Proper Functionality
To ensure that the popup is displayed correctly after adding a product to the cart via AJAX, make sure that you have properly configured the extension’s settings and that no conflicts exist with other extensions or customizations in your theme.
Remember to clear the cache after making changes by running:
php bin/magento cache:flush
Conclusion
Implementing an AJAX-powered add-to-cart functionality with a custom popup box in Magento is quite achievable using the VS-AJAX add to cart extension. By following the steps outlined above, you can easily display your desired message and buttons within the popup after adding products to the cart without refreshing the page.
For more detailed information on working with extensions and customizing templates in Magento, consider checking out our Magento Developer Guide.