Solving “Add to Wishlist” Redirection Issue in Magento
If you’re facing an issue where clicking on the “Add to Wishlist” button in your Magento store redirects users to the wishlist page instead of keeping them on the current product page, you’ve come to the right place.
The Common Problem
By default, when a user clicks this button, Magento immediately redirects them to the wishlist summary page. This can lead to a poor user experience, especially if they haven’t finished browsing other products or want to compare items before adding them.
Why It Happens
This behavior is built into the core Magento functionality. The wishlist module is designed to provide a quick overview of all added items upon clicking the “Add to Wishlist” button.
The Desired Solution
However, you might prefer a more intuitive and seamless user experience where:
- Users can add products to their wishlist while staying on the current page.
- Only after completing the action do they see a success message or have an option to view the wishlist if desired.
Step-by-Step Fix
To achieve this, we’ll need to modify the default “Add to Wishlist” functionality. Here’s a step-by-step guide:
-
Override the core template: In your theme directory (e.g.,
app/design/frontend/Vendor/Theme), create a new directory structure that mirrors the Magento core path (magento/module-wishlist/view/frontend/templates/catalog/product/compare.phtml). Place an emptycompare.phtmlfile inside this directory. -
Modify the wishlist button: Open your theme’s
catalog_product_view.xmllayout file (usually located inapp/design/frontend/Vendor/Theme/Magento_Catalog/layout) and add the following code:
<referenceBlock name="product.info.wishlist" template="Magento_Wishlist::wishlist/button.phtml"/>
This tells Magento to use a custom wishlist button template.
- Create the custom template: In your theme directory, create a new file called
button.phtmlinside thecatalog/product/wishlistfolder (create these folders if they don’t exist). Add the following code:
<button type="button" class="action-toggler wishlist-button" id="<?php /* @escapeNotVerified */ echo $block->getButtonId(); ?>" data-action="<?php /* @escapeNotVerified */ echo $block->getAction(); ?>"><span><?php /* @escapeNotVerified */ echo $block->getLabel() ?></span></button>
<script type="text/javascript">
require(['jquery', 'Magento_Wishlist/js/wishlist-button'], function ($, wishlistButton) {
$(document).ready(function () {
wishlistButton.init();
});
});
</script>
- Use AJAX to add items: Open the
wishlist-button.jsfile located invendor/magento/module-wishlist/view/frontend/web/js/wishlist-button.js. Search for theaddToWishlist()function and replace it with the following code:
addToWishlist: function (productId) {
var form = $(this).closest('form');
if (!form.length) {
return false;
}
$.ajax({
type: "POST",
url: window.checkout.wishlist.saveProduct,
data: { product_id: productId },
success: function (response, textStatus) {
form[0].reset();
alert("Item added to wishlist.");
}
});
}
This code uses AJAX to add the item to the wishlist without redirecting.
Testing Your Changes
After making these modifications and clearing your cache, clicking on the “Add to Wishlist” button should now keep users on the current page while adding items to their wishlist. The item will be added successfully, and a success message will appear without any redirection.
Remember, always backup your files before making changes in production environments. This approach modifies core Magento behavior, so it’s essential to test these modifications thoroughly.
Conclusion
In this post, we explored how to customize the “Add to Wishlist” functionality in Magento for a better user experience. By overriding templates and modifying core JavaScript functions, we achieved our goal of keeping users on the same page after adding items to their wishlists.
If you found this solution helpful, consider exploring more advanced Magento customizations at Laravel Company. We specialize in providing top-notch solutions for businesses looking to optimize their eCommerce platforms.