Forums / Contribution-Writing Guidelines / Overriding functions?

Overriding functions?

Results 1 to 5 of 5
12 Feb 2012, 15:19
#1
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Posts:
935
Plugin Contributions:
1

Overriding functions?

Is there a way to overwrite a function like zen_get_buy_now_button? Do I copy the function into an extra file with the same name or a different name?
12 Feb 2012, 15:23
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Posts:
62,757
Plugin Contributions:
1

Re: Overriding functions?

No there are no overrides for functions ... you would need to customize the original file ... save a copy of the original and your changed file for comparisons to future upgrades ...
15 Feb 2012, 00:26
#3
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Posts:
13,980
Plugin Contributions:
46

Re: Overriding functions?

Ajeh:

No there are no overrides for functions ...


Just like everything else in Zen Cart, where there's a will there's a way! I've used the method I'll describe for a while now, based on a post that I found once and can't resurrect. It's a bit convoluted, but it does provide an override mechanism.

The function zen_get_buy_now_button exists in functions_general.php.

Step 1: Copy /includes/init_includes/init_general_functions.php to /includes/init_includes/overrides/init_general_functions.php. Edit that file

<?php
/**
 * load the system wide functions
 * see {@link  http://www.zen-cart.com/wiki/index.php/Developers_API_Tutorials#InitSystem wikitutorials} for more details.
 *
 * @package initSystem
 * @copyright Copyright 2003-2005 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: init_general_funcs.php 2845 2006-01-13 06:49:15Z drbyte $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}
/**
 * General Functions
 */
require(DIR_WS_FUNCTIONS . 'overrides/functions_general.php');
/**
 * html_output functions (href_links, input types etc)
 */
require(DIR_WS_FUNCTIONS . 'html_output.php');
/**
 * basic email functions
 */
require(DIR_WS_FUNCTIONS . 'functions_email.php');
/**
 * EZ-Pages functions
 */
require(DIR_WS_FUNCTIONS . 'functions_ezpages.php');
/**
 * User Defined Functions
 */
include(DIR_WS_MODULES . 'extra_functions.php');
?>


Step 2: Create the directory /includes/functions/overrides. Copy /includes/functions/functions_general.php into that folder and edit it to your heart's content.

The other functions can be overridden in a similar manner.
15 Feb 2012, 01:25
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,506
Plugin Contributions:
173

Re: Overriding functions?

Just remember that if you do that, your alterations are NOT multi-version friendly. So will absolutely require changes when a new version is released, to ensure compatibility.

And they're not compatible with other overrides done by the storeowner, if they've already created an override for init_general_functions
15 Feb 2012, 14:55
#5
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Posts:
13,980
Plugin Contributions:
46

Re: Overriding functions?

DrByte:

Just remember that if you do that, your alterations are NOT multi-version friendly. So will absolutely require changes when a new version is released, to ensure compatibility.

And they're not compatible with other overrides done by the storeowner, if they've already created an override for init_general_functions


DrByte, I'm not necessarily proposing that this approach be used for general add-on writers for the reasons you mentioned. I've found it useful in my one-off changes for my site(s) since the overrides that I've created are preserved on an upgrade.