The best approach for the template is still using ocmod, but for this specific case, there is still a workaround with Event. Let's just assume the code was in the product template.
Code: Select all
<?php
public function adminViewProductProductFromBefore(&$route, &$data, &$template)
{
/**
* Assuming:
* $data['customer_group']['customer_group_id'] = 2;
* $data['product_special']['customer_group_id'] = 2;
*
* In the template, both value is equal, condition return true
* {% if customer_group.customer_group_id == product_special.customer_group_id %}
*
* New condition:
* $other_variable = false;
*
* Q: How to make the condition in template false?
*/
if ($data['customer_group']['customer_group_id'] == $data['product_special']['customer_group_id'] && !$other_variable) {
// Condition in template is false because both "customer_group_id" no longer equal
$data['product_special']['customer_group_id'] = (int)$data['customer_group']['customer_group_id'] + 1;
}
/**
* Assuming:
* $data['customer_group']['customer_group_id'] = 2;
* $data['product_special']['customer_group_id'] = 3;
*
* In the template, both value is different, condition return false
* {% if customer_group.customer_group_id == product_special.customer_group_id %}
*
* New condition:
* $other_variable = true;
*
* Q: How to make the condition in template true?
*/
if ($data['customer_group']['customer_group_id'] != $data['product_special']['customer_group_id'] && $other_variable) {
// Condition in template become true, because both "customer_group_id" is equal
$data['product_special']['customer_group_id'] = $data['customer_group']['customer_group_id'];
}
// Cautions: use wisely or someone going to spend the night in headache debugging it :D
}
The reason why Events introduced was, I think because there is a lot of code conflict when several vQmod/OcMod change the same file. It's hard to think in a more efficient way when you can do whatever you want, to CRUD code directly with OcMod in a way that the whole thing becomes fragile.
There is a time when I think that it would be great if OcMod <add> tags have characters limit, force us to think in the most efficient way.
Code: Select all
// OcMod to change product price, use OcMod similar to Event listener
// Make the method implement the single-responsibility principle.
list($price, $special, $tax) = $this->load->controller('extension/module/my_module/updateProductPrice', array($data['price'], $data['special'], $data['tax']));
And we can do whatever we want at
my_module::updateProductPrice() while keeping the change to core files as minimum as possible. And imagine how many lines of code, caffeine, and aspirin we can save to change product price at the categories, view cart, checkout, etc.
IMO, the "
$this->load->controller()" is the most usefull OpenCart library to be combined with OcMod. Even if we need to load several models, use complex condition, and do NASA like algorithm calculation; it can be replaced with
$this->load->controller('extension/module/my_module/checkThoroughly') and do those check at
my_module::checkThoroughly() then return a simple primitive value.
But of course, if Event was hooked manually like how WordPress does, then things can be easier. Add them several dozen Event "hook" for each Opencart release, in a few releases we can have a hundred of them.
Code: Select all
list($price, $special, $tax) = $this->event->trigger('catalog/controller/product/product/mainPrice', array($data['price'], $data['special'], $data['tax']));