I'm trying to implement adding product to wishlist using ajax. I have managed to do this but I can't get the success message from response ("You have added X product to wishlist").
require([
'jquery',
], function(jQuery){
(function($) {
$(document).ready(function(){
$('#product-addtowishlist-button').click(function(){
var parsedJson = $.parseJSON($(this).attr( "dtcustom" ));
var url = parsedJson.action;
var data1 = parsedJson.data;
var current_prod = data1.product;
var current_uenc = data1.uenc;
formKey = $('input[name="form_key"]').val();
var data = {
action : "add-to-wishlist",
form_key : formKey,
product : current_prod,
uenc : current_uenc
}
$.ajax({
url: url,
data: data,
beforeSend: function(xhr, options) {
jQuery('#loading').show();
},
success: function(data,textStatus,jqXHR){
if (textStatus == 'success') {
//can't find success message in response html
}
}
});
return false;
});
});
})(jQuery);
});
Adding product to wishlist usually redirects to Wishlist page, displaying a success message. But in this case response data does not contain that message.
Is there a better way to do the ajax call? Am I missing something here?
Best Answer
First of all override your wish-list add controller by your custom module. after that use below code for your add controller. Currently I'm using Vendor\Module as my custom module, you can use anything.
After that please add following code in your ajax success.
Please try this and let me know if you have any question.
Second Option
Please use this extension for AJAX wishlist if you don't want to use your custom module : https://www.tigren.com/magento-2-extensions/ajax-suite-magento-2/