Post by mokiwaa » Sat Sep 26, 2020 7:50 am

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

Newbie

Posts

Joined
Wed Apr 17, 2019 6:10 am

Post by Cue4cheap » Sat Sep 26, 2020 10:56 pm

Huh?
Please slow down a bit and explain further.
Thank you,
Mike

cue4cheap not cheap quality


Active Member

Posts

Joined
Fri Sep 20, 2013 4:45 am

Post by straightlight » 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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by mokiwaa » Tue Sep 29, 2020 9:38 am

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.

Newbie

Posts

Joined
Wed Apr 17, 2019 6:10 am

Post by sw!tch » 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.

Backup and [ learn how to recover ] before you make any changes.


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm

Post by letxobnav » Tue Sep 29, 2020 11:01 am

pointless to use events for peanut changes like this.

Crystal Light Centrum Taiwan
Extensions: MailQueue | SUKHR | VBoces

“Data security is paramount at [...], and we are committed to protecting the privacy of anyone who is associated with our [...]. We’ve made a lot of improvements and will continue to make them.”
When you know your life savings are gone.


User avatar
Expert Member

Posts

Joined
Fri Aug 18, 2017 4:35 pm
Location - Taiwan

Post by mokiwaa » Wed Sep 30, 2020 1:37 am

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...

Newbie

Posts

Joined
Wed Apr 17, 2019 6:10 am

Post by mokiwaa » Wed Sep 30, 2020 1:44 am

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.

Newbie

Posts

Joined
Wed Apr 17, 2019 6:10 am

Post by straightlight » Wed Sep 30, 2020 2:22 am

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Wed Sep 30, 2020 7:30 am

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.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: thekrotek and 26 guests