Opencart Event system OC 3.0.2.0, override core controller and twig file?

1.6k Views Asked by At

Is it possible to use only the OC Event system for these overrides?

I need to override the core index function of the product controller. I want to edit the core file and add this line:

$data['quantity'] = $product_info['quantity']; 

to the function index() in "class ControllerProductProduct extends Controller"

and then I need add this line in product.twig file:

<span>{{ quantity }}</span>

I want to use only the OC Event system, not ocmod or vqmod.

It is real?

Thanks for help.

1

There are 1 best solutions below

1
On

First you need to add your event into database, you can use addEvent function inside your module install function, like this:

public function install(){
    $this->load->model('setting/event');
    $this->model_setting_event->addEvent('my_module', 'catalog/view/product/product/after', 'extension/module/my_module/edit_product_page');
}

Or add it manually:

INSERT INTO `oc_event` (`code`, `trigger`, `action`, `status`) VALUES ('my_module', 'catalog/view/product/product/after', 'extension/module/my_module/edit_product_page', 1);

Now create this file:

catalog\controller\extension\module\my_module.php

And its contents:

<?php
class ControllerExtensionModuleMyModule extends Controller {
    public function edit_product_page(&$route = '', &$data = array(), &$output = '') {
        if (isset ($this->request->get['product_id'])) {
            $product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
            if ($product_info) {
                // Display it before <li>Product Code:
                $find = '<li>' . $data['text_model'];
                $replace = '<li>Quantity: ' . $product_info['quantity'] . '</li>' . $find;
                $output = str_replace($find, $replace, $output);
            }
        }
    }
}

Result:

MacBook

Brands Apple

Quantity: 929

Product Code: Product 16

Reward Points: 600

Availability: In Stock