Magento 2 notification message formatting
-
25-02-2021 - |
Soluzione
I have the exact same business requirement. It is not possible to add html tags in translation csv file but you may enter specific unique character in csv file for specific message. So I add jQuery code to do this work, I know it is not the best practice but it worked for me. As there is no answer yet against this question so I share what I did.
First you have to add unique specific characters (strong) and (/strong) in your theme translation csv file and string variable between those specific characters like
You added %1 to your shopping cart.","You added (strong)%1(/strong) to your shopping cart.
Then Add this jQuery code in your JS file
jQuery(document).ajaxStop(function() {
var notificationMessage = jQuery.trim(jQuery("#maincontent.page-main.container > .messages .messages").find( "div:eq(1)" ).text());
if(notificationMessage != ""){
setTimeout(function () {
var strong = jQuery('#maincontent.page-main.container > .messages .messages:contains("(strong)")').html();
if(typeof strong !== 'undefined'){
strong = strong .replace("(strong)", "<strong>");
strong = jQuery.trim(strong.replace("(/strong)", "</strong>"));
}
jQuery('#maincontent.page-main.container > .messages .messages:contains("(strong)")').html(strong);
}, 200);
}
});
I hope this will help
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange