Page 1 of 1

Help With my first event module

Posted: Sat Oct 17, 2020 5:46 am
by sukhbirgs
Hello, i am trying to write a simple event which triggers before product is added to cart and change model number in data based on product name. Created 2 files

upload/admin/controller/extension/module/eve_model.php

Code: Select all

<?php
class ControllerExtensionModuleEveModel extends Controller {
	private $error = array();    
    public function index() {}
	public function install() {
        /* Adding the events */
        $this->load->model('setting/event');
        $this->model_setting_event->addEvent('eve3x_model_update', 'catalog/model/checkout/order/addOrder/before', 'extension/module/eve_model/updateModel');
		
	}

	public function uninstall() {
		$this->load->model('setting/event');
		$this->model_setting_event->deleteEventByCode('eve3x_model_update');
	}
}
upload/catalog/controller/extension/module/eve_model.php

Code: Select all

<?php 
class ControllerExtensionModuleEveModel extends Controller
{

	// Event Hook update model number
	public function updateModel(&$route, &$data) {
		if (isset($data['products'])) {
			foreach ($data['products'] as &$product) {
				if(strpos($product['name'], 'EVE') === true){	
					foreach ($product['option'] as $option) {
						if(strpos($product['model'], $option['value']) === false){
								$product['model'] .=  "-".$option['value'];
						}
					}
				}
				
			}
		}
	}
}

Zip as ocmod file and install, my event is not being registered. If its registered and triggered then i can debug, but it not registering ( showing in event list, or trigger when i enter product to cart). Thanks for your help

Re: Help With my first event module

Posted: Sat Oct 17, 2020 8:02 am
by sw!tch
It registers fine: You can also verify in the DB using the oc_event table. As for what you are doing with the code and model number that's untested.

Code: Select all

Trigger
catalog/model/checkout/order/addOrder/before

Action
extension/module/eve_model/updateModel
The example you have is using a trigger on the addOrder Method so not sure what you mean by "enter product to cart".

You can write to your log file to debug if needed.

Code: Select all

public function updateModel(&$route, &$data) {
$this->log->write('ControllerExtensionModuleEveModel -> updateModel(): '. print_r($data, true));
....

Re: Help With my first event module

Posted: Sat Oct 17, 2020 9:10 am
by straightlight
This line can be tricky at times:

Code: Select all

if(strpos($product['name'], 'EVE') === true){	
I would use:

Code: Select all

if (strpos(html_entity_decode(strtoupper($product['name']), ENT_QUOTES, 'UTF-8'), 'EVE')) {

Re: Help With my first event module

Posted: Tue Oct 20, 2020 2:52 am
by sukhbirgs
Hello, thanks @Sw!tch and @straightlight for taking time to answer my question. I have never used events on oc, so was not sure how to register them. Went thru quite a few tutorials, they have all the info but how to upload (bundle) ur code and install them. So i tried creating a module bu putting the file in appropriate folders as mentioned above and installed it. Got confirmation that module is installed but my events are not being registered.
Thats my first issue, both the files and location are listed above.

Now moving onto issue at hand, i am using "Related Product Options Pro" and this module is working fine. But we have one product that just needs little bit extra work to git right model. In this product we have some extra options that customer can add onto products , so you select all the options listed and model number is created and then there are some extra options like extra memory, usb connection etc that can be added onto the product. And this part works, model number is added to cart and extra options are also listed in product options. But we want to add extra options code at the end of the product model. It makes easier for production staff to assemble product based on model.

I have it working it by using ocmod

Code: Select all

 <file path="catalog/model/checkout/order.php">
    <operation error="log">
      <search><![CDATA[foreach ($data['products'] as $product) {]]></search>
      <add position="after"><![CDATA[
      // Change eve model
				//if(strpos($product['name'], 'EVE') === true){	
		if (strpos(html_entity_decode(strtoupper($product['name']), ENT_QUOTES, 'UTF-8'), 'EVE')) {
			foreach ($product['option'] as $option) {
			// compare options value thats added to model if it exist in model, if not add it at the end
				if(strpos($product['model'], $option['value']) === false){
						$product['model'] .=  "-".$option['value'];
				}
			}
		}
    // end eve
    ]]></add>
    </operation>
</file>
	
<file path="system/library/cart/cart.php">
	<operation error="log">
      <search><![CDATA[if ($option_value_query->num_rows) {]]></search>
      <add position="before"><![CDATA[
      // status chage eve model
		if(strpos($product_query->row['name'], 'EVE3x') !== false){	
		// compare options value thats added to model if it exist in model, if not add it at the end
			if(strpos($product_query->row['model'], $option_value_query->row['name']) === false){
				$product_query->row['model'] .=  "-".$option_value_query->row['name'];
			}
		}			
    // end eve
    ]]></add>
    </operation>
  </file>
This have solved my problem, but still if you can guide me how to register events that would be great. @Sw!tch had mentioned i can check in oc_event table and i can confirm after installing my module event was not in this table.

Thanks again for your time.

Re: Help With my first event module

Posted: Tue Oct 20, 2020 3:03 am
by straightlight
This have solved my problem, but still if you can guide me how to register events that would be great. @Sw!tch had mentioned i can check in oc_event table and i can confirm after installing my module event was not in this table.

Thanks again for your time.
How to register events can be found on this wiki documentation: https://github.com/opencart/opencart/wiki/Events-System .

Re: Help With my first event module

Posted: Tue Oct 20, 2020 3:15 am
by sw!tch
Further it only installs when you install the module. So if you make changes you have to uninstall the module and then install it again.

In addition to the uninstall method, I also remove any previous stored events on install. You can sometimes end up with duplicates, etc.

Code: Select all

public function install() {

	/* Adding the events */

	$this->load->model( 'setting/event' );
	$this->model_setting_event->deleteEventByCode( 'eve3x_model_update' );  // remove any previous stored events.

	$this->model_setting_event->addEvent( 'eve3x_model_update', 'catalog/model/checkout/order/addOrder/before', 'extension/module/eve_model/updateModel' );

}