Page 1 of 1

Stop action and Return message alert with opencart event

Posted: Sat Sep 26, 2020 7:50 am
by mokiwaa
I want to make model field unique and I have create a event for this! My dificulty is to stop adding or update action and return an error if the model already exist in event model before addProduct or before editProduct.

Any suggestions will be greatly appreciated,

OC Version:3.0.2.0

Re: Stop action and Return message alert with opencart event

Posted: Sat Sep 26, 2020 10:56 pm
by Cue4cheap
Huh?
Please slow down a bit and explain further.
Thank you,
Mike

Re: Stop action and Return message alert with opencart event

Posted: Sun Sep 27, 2020 2:40 am
by straightlight
The $this->session->data['error'] does not exist in the core from admin/controller/catalog/product.php file. You'd need to create this session super global name in your /before event. Then, in your /after event, to output the captured error into your $output referenced variable into your TWIG string or file and unset the error key from the session super global. This can also be accomplished without the $this->session->data by simply outputting the error message string into your $output from the /after once redirected.

More information on how to properly create an event can be found on this documentation: https://github.com/opencart/opencart/wiki/Events-System . Otherwise, you can always create a new service request in the Commercial Support section of the forum or contact a reliable Opencart Partner from the top Resources link of this site to get this done as a custom job.

Re: Stop action and Return message alert with opencart event

Posted: Tue Sep 29, 2020 9:38 am
by mokiwaa
straightlight wrote:
Sun Sep 27, 2020 2:40 am
The $this->session->data['error'] does not exist in the core from admin/controller/catalog/product.php file. You'd need to create this session super global name in your /before event. Then, in your /after event, to output the captured error into your $output referenced variable into your TWIG string or file and unset the error key from the session super global. This can also be accomplished without the $this->session->data by simply outputting the error message string into your $output from the /after once redirected.

More information on how to properly create an event can be found on this documentation: https://github.com/opencart/opencart/wiki/Events-System . Otherwise, you can always create a new service request in the Commercial Support section of the forum or contact a reliable Opencart Partner from the top Resources link of this site to get this done as a custom job.
I have tried what you have sugested but the after event seems dont work...
I have create this before event and is working:

Code: Select all

$this->model_setting_event->addEvent("check_model_unique","admin/controller/catalog/product/add/before","extension/module/my_custom_settings/checkModelUnique");
after that i have created the after event as sugested like this:

Code: Select all

$this->model_setting_event->addEvent("check_model_uniquea","admin/controller/catalog/product/add/after","extension/module/my_custom_settings/checkModelUniquea");
and the respective method:

Code: Select all

public function checkModelUniquea(&$route, &$args, &$output) {
	    	$this->log->write("teste");	    			
}
When I click the add button the after event is fire up, but when I click save to try to add the product he never fire up and he add the product in database and go to product list.
I want to stop before addProduct is executed and redirect to the add form product with the error.
I can do that with jquery before submit the form i call with ajax an method and return the error, but i want to do that with an event and without jquery/ajax.

Re: Stop action and Return message alert with opencart event

Posted: Tue Sep 29, 2020 10:03 am
by sw!tch
Do you want all input carried back to the view or just halt execution? Simple to just stop execution of the code, if you want an error message on model needing to be unique its more involved.

As much as I prefer events, this can be done with ocmod on the validate method in few lines of code.

Re: Stop action and Return message alert with opencart event

Posted: Tue Sep 29, 2020 11:01 am
by letxobnav
pointless to use events for peanut changes like this.

Re: Stop action and Return message alert with opencart event

Posted: Wed Sep 30, 2020 1:37 am
by mokiwaa
sw!tch wrote:
Tue Sep 29, 2020 10:03 am
Do you want all input carried back to the view or just halt execution? Simple to just stop execution of the code, if you want an error message on model needing to be unique its more involved.

As much as I prefer events, this can be done with ocmod on the validate method in few lines of code.
All input carried back to the view with the error message needing to be unique it will be more nice for the user when adding product. In my case I dont have a lot of input, because i have disabled many of them, but if you fill all the input that come with opencart and you receive a message that the model need to be unique and you need to fill again those input , this will be in a nightmare.. :)
What a learn after reading, if I want to stop the execution of the code is simple, just need to return $data on the event...

Re: Stop action and Return message alert with opencart event

Posted: Wed Sep 30, 2020 1:44 am
by mokiwaa
letxobnav wrote:
Tue Sep 29, 2020 11:01 am
pointless to use events for peanut changes like this.
From https://github.com/opencart/opencart/wi ... ion-System
As of OpenCart 3.1.0.0+ OCMOD will be removed from the OpenCart source code. Only the event system will be used for modifications.
This is why I want to learn how we can use the event system without using ocmod, vqmod or edit "system" file of opencart.

Re: Stop action and Return message alert with opencart event

Posted: Wed Sep 30, 2020 2:22 am
by straightlight
The provided wiki documentation will show you how. If you are not sure how to accomplish this, you could always create a new service request in the Commercial Support section of the forum or contact a reliable OC partner from the Resources link on the top of the site to get this done as a custom job.

Re: Stop action and Return message alert with opencart event

Posted: Wed Sep 30, 2020 7:30 am
by straightlight
If you want to use a before event, then there's no need to switch to /after.

Simply replace:

Code: Select all

public function checkModelUniquea(&$route, &$args, &$output) {
with:

Code: Select all

public function checkModelUniquea(&$route, &$args) {
    if ($this->request->server['REQUEST_METHOD'] == 'POST' && $route == 'model/catalog/product/addProduct') {
        // Your action here.
    }
}
Then, replace /after with /before in your database.

In this scenario, if you want to use a /after, you could always pull the new $product_id added on your database from the model.

Code: Select all

public function checkModelUniquea(&$route, &$args, &$output) {
    if ($this->request->server['REQUEST_METHOD'] == 'POST' && $route == 'model/catalog/product/addProduct') {
       // Last product ID.
       $this->load->model('catalog/product');
       
       $product_id = $this->model_catalog_product->addProduct($args);
    }
}
As per this e.g: https://github.com/opencart/opencart/co ... 470f2cdR61 , you could also replace from the above:

Code: Select all

if ($this->request->server['REQUEST_METHOD'] == 'POST' && $route == 'model/catalog/product/addProduct') {
with:

Code: Select all

if ($this->request->server['REQUEST_METHOD'] == 'POST' && isset($this->request->get['route']) && $this->request->get['route'] == 'model/catalog/product/addProduct') {
If you'd like to keep the $route variable, you could switch the controller route /before from your database:

Code: Select all

admin/controller/catalog/product/add/before
to read:

Code: Select all

admin/model/catalog/product/addProduct/before
Then, you could use:

Code: Select all

if ($route == 'model/catalog/product/addProduct') {
    // Your action here.
}
As per this e.g: https://github.com/opencart/opencart/bl ... ncy.php#L8 .

Also assuming your extension module's filename is conformed to the one you entered on the database in your events table.