I want to write a function for catalog->controller->checkout->cart.php in Opencart 2.3.0.2 I have already written a logic to check priduct_id and action to be taken if particular product id is found. Now I want to take this logic in separate function in separate php file so that it will become more manageable.
I created function in php file under system->helper and loaded it from startup.php. Then I can call this function from cart.php but reference to variable $this is lost even if I am passing $this to this function.
My stripped down code looks like this cart.php
//some code before this
if (!$json) {
// Check if Product is Addon
test($this);
//more code after this
customfunction.php
function test($this) {
// print_r("Test Called");
$temp = $this->request->post['product_id'];
if ($this->request->post['product_id'] == 142) {
$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
$product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']);
$product_option_id = $product_option['product_option_id'];
//more code
I am getting error for
$this->request->post['product_id'];
Can someone tell me how to call custom function from separate php file preserving reference to $this variable.