Forums / Contribution-Writing Guidelines / Overriding functions?

Overriding functions?

Started by retched on 12 Feb 2012, 3:19 PM · Views: 2,605

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

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

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, 3:23 PM
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
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 ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

15 Feb 2012, 12:26 AM
#3
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,129
Plugin Contributions:
56

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, 1:25 AM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

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

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

15 Feb 2012, 2:55 PM
#5
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,129
Plugin Contributions:
56

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.