跳至內容
選單
此問題已被標幟
1 回覆
4566 瀏覽次數

I develop a website and I use Newsletter module. By default, it shows success message (Thanks for subscribing!) when you click to Subscribe button. I don't want to see these toast messages for Newsletter module.


I see that this toast message comes from the code below of the website_mass_mailing.js file. 


self.displayNotification({ 
    type: toastType, 
    title: toastType === 'success' ? _t('Success') : _t('Error'), 
    message: result.toast_content, 
    sticky: true,
});


When I comment out this part, it doesn't show the toast message. But I want to do it with inheriting this file and not changing the original one. I tried to inherit it but, I couldn't succeed. Any suggestion how to do it?

頭像
捨棄
最佳答案

Hi George, 

Please check with the below code snippet.

To add the changes, you'll basically have to override the existing function by monkeypatching the code.

odoo.define('your_module.name', function (require){

var publicWidget = require('web.public.widget');

publicWidget.registry.subscribe.include({
_onSubscribeClick: async function () {
var self = this;
var $email = this.$(".js_subscribe_email:visible");

if ($email.length && !$email.val().match(/.+@.+/)) {
this.$target.addClass('o_has_error').find('.form-control').addClass('is-invalid');
return false;
}
this.$target.removeClass('o_has_error').find('.form-control').removeClass('is-invalid');
let tokenObj = null;
if (this._recaptcha) {
tokenObj = await this._recaptcha.getToken('website_mass_mailing_subscribe');
if (tokenObj.error) {
self.displayNotification({
type: 'danger',
title: _t("Error"),
message: tokenObj.error,
sticky: true,
});
return false;
}
}
const params = {
'list_id': this.$target.data('list-id'),
'email': $email.length ? $email.val() : false,
};
if (this._recaptcha) {
params['recaptcha_token_response'] = tokenObj.token;
}
this._rpc({
route: '/website_mass_mailing/subscribe',
params: params,
}).then(function (result) {
let toastType = result.toast_type;
if (toastType === 'success') {
self.$(".js_subscribe_btn").addClass('d-none');
self.$(".js_subscribed_btn").removeClass('d-none');
self.$('input.js_subscribe_email').prop('disabled', !!result);
if (self.$popup.length) {
self.$popup.modal('hide');
}
}
// make the changes you need accordingly or comment out the below code.
self.displayNotification({
type: toastType,
title: toastType === 'success' ? _t('Success') : _t('Error'),
message: result.toast_content,
sticky: true,
});
});
},

})

})

Regards

頭像
捨棄
作者

Hi Cybrosys Techno Solutions Pvt.Ltd,

thanks for the answer. It was giving an error like this:

Traceback:
ReferenceError: _t is not defined
at http://localhost:8069/web/content/635-0ce3d45/1/web.assets_frontend_lazy.js:372:57

So I add `var _t = core._t;` before `var publicWidget` line and it worked. Appreciate!

相關帖文 回覆 瀏覽次數 活動
3
7月 18
3705
2
7月 17
4396
0
5月 24
1925
1
8月 23
3505
1
8月 23
11061