Overriding Hikashop classes for custom features

  • Posts: 292
  • Thank you received: 5
  • Hikashop Business
12 years 5 months ago #28425

In helper.php line 760 would it be possible to check for the existance of a class file in either the site or admin template locations?
The purpose would be to reduce the risk of any class customisations a developer may have made getting accidently overridden during a Hikashop upgrade.
I.E. change code

if(!class_exists($className)) include_once(constant(strtoupper('HIKASHOP_'.$group)).$class.'.php');
to something like this:
if(!class_exists($className)) {
  $app = JFactory::getApplication();
  $classfile= JPATH_SITE . DS . 'templates'.DS.$app->getTemplate().DS.'classes'.DS.'com_hikashop'.DS.$class.'.php'
  if (file_exists($classfile)) {
    // Add some way of checking if custom class is compatible with installed Hikashop version
    // If not then produce an error
    include_once($classfile);
  }
  else {
    $classfile= JPATH_ADMINISTRATOR . DS . 'templates'.DS.$app->getTemplate().DS.'classes'.DS.'com_hikashop'.DS.$class.'.php'
    if (file_exists($classfile)) {
      // Add some way of checking if custom class is compatible with installed Hikashop version
      // If not then produce an error
      include_once($classfile);
    }
  }
  if(!class_exists($className)) include_once(constant(strtoupper('HIKASHOP_'.$group)).$class.'.php');
This idea came about while thinking of possible solutions to the following topics:
11199-coupons-and-discounts
27448-coupons-for-specific-products

Another idea was for a couple of new functions which could be called by a plug-in:
onBeforeVoucherAdded
onAfterVoucherAdded

These would be invoked in loadAndCheck function of discount.php something like this:
function loadAndCheck($coupon_code,&$total,$zones,&$products,$display_error=true){
  $coupon = $this->load($coupon_code);
  if (...onBeforeVoucherAdded... exists) {
    $beforeResult = ...->onBeforeVoucherAdded($coupon,$total,$zones,$products,$display_error);
    if ($beforeResult === FALSE) return null;
    if (is_object($beforeResult)) return $beforeResult;
  }
  $result = $this->check($coupon,$total,$zones,$products,$display_error);
  if (...onAfterVoucherAdded... exists) {
    $afterResult = ...->onAfterVoucherAdded($result,$coupon,$total,$zones,$products,$display_error);
    if ($afterResult === FALSE) return null;
    if (is_object($afterResult)) return $afterResult;
  }
  return $result;
}

Any other ideas if how this moght be achieved?

Please Log in or Create an account to join the conversation.

  • Posts: 81484
  • Thank you received: 13062
  • MODERATOR
12 years 5 months ago #28444

The current code already allow you to override all the classes in HikaShop.
You can just create a joomla system plugin and implement the class in the plugin file. Since the code checks that the class does not already exists before loading the file in HikaShop and since the system plugins are always loaded first, you will be able to achieve just what you're looking for.
So I don't feel like that code is needed actually.

Regarding the addition of events on the coupon checking code, that's a nice idea even though I doubt that it will be used a lot. We'll implement something for that (not like that).

Please Log in or Create an account to join the conversation.

  • Posts: 292
  • Thank you received: 5
  • Hikashop Business
12 years 5 months ago #28492

Looks like a system plug-in along the following lines will do the job:

class plgSystemMyclasses extends JPlugin {
  public function onAfterRoute() {
    if (JRequest::getVar('option') == 'com_hikashop') {
      if (!class_exists('hikashop')) {
        include(JPATH_ADMINISTRATOR . DS . 'components/com_hikashop/helpers/helper.php');
      }
      include(__DIR__ . DS . '/class/myclasses.php');
    }
  }
}

Please Log in or Create an account to join the conversation.

  • Posts: 81484
  • Thank you received: 13062
  • MODERATOR
12 years 5 months ago #28501

Yes. You could even do simpler:

Do not have that code in your plugin file and directly have your classes there.
When joomla starts loading, it will include your plugin file and your classes will be instantiated automatically and since it won't find your plugin class it won't try to call it but your classes will already be there for HikaShop to use.

Please Log in or Create an account to join the conversation.

Time to create page: 0.055 seconds
Powered by Kunena Forum