Search code examples
opencartopencart-3

How to get the product_id on the product_add_after event?


I have a module which is triggered on the after_product_add event in OpenCart 3.0.3.1. In the code I want to add information to the product, but to do this I need the product_id, which is not included in the array of data I get from the event.

Does anybody know a solution besides changing the source code of OpenCart?

Background information: The module will sync products with another system, on creation I want to store a guid in the database. To do this I need the product_id.

The array I get from the event trigger is: Array ( [0] => Array ( [product_description] => Array ( [1] => Array ( [name] => test25 [description] => [meta_title] => test25 [meta_description] => [meta_keyword] => [tag] => )

            )

        [model] => test25
        [sku] => 
        [upc] => 
        [ean] => 
        [jan] => 
        [isbn] => 
        [mpn] => 
        [location] => 
        [price] => 
        [tax_class_id] => 0
        [quantity] => 1
        [minimum] => 1
        [subtract] => 1
        [stock_status_id] => 6
        [shipping] => 1
        [date_available] => 2019-06-04
        [length] => 
        [width] => 
        [height] => 
        [length_class_id] => 1
        [weight] => 
        [weight_class_id] => 1
        [status] => 1
        [sort_order] => 1
        [manufacturer] => 
        [manufacturer_id] => 0
        [category] => 
        [filter] => 
        [product_store] => Array
            (
                [0] => 0
            )

        [download] => 
        [related] => 
        [option] => 
        [image] => 
        [points] => 
        [product_reward] => Array
            (
                [1] => Array
                    (
                        [points] => 
                    )

            )

        [product_seo_url] => Array
            (
                [0] => Array
                    (
                        [1] => 
                    )

            )

        [product_layout] => Array
            (
                [0] => 
            )

    )

)

I have searched forums and developer documentation. The only references I find tell me there is no more information than what I already have. I tried to get the product id from the request

public function createproduct($route, $args) {
    if ($this->config->get('module_umrlzconn_status')) {
        if ($this->config->get('module_umrlzconn_productsync')) {
            $this->load->model('extension/module/umrlzconn');
            file_put_contents(DIR_STORAGE .'UMRLZ/prodcreate.log', print_r($args,true));
            $productguid = $this->NewGuid();
            $this->model_extension_module_umrlzconn->setProductGuid($this->request->get['product_id'], $productguid);
        }
    }
}

I'd like to get a product_id for the product that was just created, so I can use it to add information to it.


Solution

  • Anyone searching for the answer, thanks to DigitCart on the OpenCart forum, the product_id is sent as a third variable with the event-action. So you can get the product id as follows:

    public function createproduct($route, $args, $product_id) {
     // Do your thing
    }