Jump to content

How To Display Error/ Successful/ Warning/ Notification Messages Up At The Back Office


mcwaloy

Recommended Posts

Hi,

 

I'm trying to update the back office when adding new product. I wrote some codes to check for some conditions when a new product is being added, so if those conditions arent met, a message should be displayed on the screen on clicking the [save and stay] or [save] button.

 

To accomplish this, I edited the function below in `classes/Product.php`. Hope I'm right and not missing anything else, but it works though. 

public function add($autodate = true, $null_values = false)

However, the problem I'm having is displaying the error or notification message after the added code is met. I used the below code.

die(Tools::displayError('Error message here'));

It shows the error message but on a completely new blank screen.

 

How can I make the message display on the same screen and properly with the current way of displaying messages in PS?

 

Please help!!! Thanks in advance

Edited by mcwaloy (see edit history)
Link to comment
Share on other sites

Hi.

 

Sadly, PrestaShop uses a lot of redirections after doing some actions. This means that when you use certain action hooks, they will be run by a controller that doesn't display anything, and then redirects to another page. This also means that anything you try to display when that hook takes place will never show. It's specially annoying for module developers.

 

This is a bad design decision that gets worse because there's not a session-based "flash messages" system in place. This is why in plenty of core's code you will see url parameters that are used to show messages through the back office (search for "&conf=" in the code to understand this).

 

In your case, because you are using die(), the message will show but the redirection will never happen, so you get a blank page with the message.

 

It is said that a proper messages system will be implemented in version 1.7, but currently you are out of luck. The only way to do it is by using url parameters on redirection pages or implement your own messages system.

Edited by gonssal (see edit history)
Link to comment
Share on other sites

Hi Gonssal,

 

Thanks a lot for your comprehensive response. With the only way out you pointed out using url parameters, could you please elaborate more on how it currently works? And how I can go by updating what I want to add to it?

 

With respect to the add function where this happens, I saw the Hook code but I dont quite get where to update in the code.

Hook::exec('actionProductSave', array('id_product' => (int)$this->id, 'product' => $this));

I went to the "Hook" class and the "exec" function as well, but it seems kinda complex. Moreso, I didnt see where "&conf=" was used in there. 

 

For example, I'd like to see where and how PS currently does their notification handling in the codes.

 

Please help me out.

 

Thanks.

Link to comment
Share on other sites

No, I can't elaborate anymore. Search for the string I said in core's code and look how it's done there, if you want to build on top of bad design decisions.

 

Also, if I understand correctly you are modifying core files directly, which is a very bad idea, because if you want to update PrestaShop all your changes will be lost. There's an override system for that.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...