Forums / General Questions / Where and how on new define approach

Where and how on new define approach

Started by dave224 on 12 Oct 2024, 9:42 PM · Views: 2,055

Results 1 to 14 of 14
12 Oct 2024, 9:42 PM
#1
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Where and how on new define approach

I'm completely lost on where to put defines and how to override core defines after reading material on this site. If someone could answer the following questions, I might be able to make sense of it. This is driven in part because I read in zen cart 3.0, all defines must be of the lang. style (if I understood it correctly) and as I migrate to zc 2.0 and above, I want to do the defines correctly to minimize pain later.

  1. where is the recommended place and filename for overriding core defines?
  2. Where is the recommended place and filename for custom defines applicable to a specific page?
  3. Where is the recommended place and filename for defines needed for functions, observers, etc. that may be applicable to multiple pages?
  4. Is recommended practice to include all defines from a core defines file in a override file or just the defines being overridden?
  5. How should current array defines be handled, including array defines that are associative arrays? (Ex: define('CONSTANT_NAME', array(key1=>value1, key2=>value2));
    Thank you!
    Dave
    zc1.5.8a, php 8.2, mysql 8.0, only one language needed - English
12 Oct 2024, 9:50 PM
#2
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: Where and how on new define approach

This might be a good jumping off point:
https://docs.zen-cart.com/user/languages/158_language_files/

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

13 Oct 2024, 12:07 PM
#3
lat9 avatar

lat9

Administrator

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

Re: Where and how on new define approach

Dave224:

I'm completely lost on where to put defines and how to override core defines after reading material on this site. If someone could answer the following questions, I might be able to make sense of it. This is driven in part because I read in zen cart 3.0, all defines must be of the lang. style (if I understood it correctly) and as I migrate to zc 2.0 and above, I want to do the defines correctly to minimize pain later.

  1. where is the recommended place and filename for overriding core defines?
  1. Where is the recommended place and filename for custom defines applicable to a specific page?
  1. Where is the recommended place and filename for defines needed for functions, observers, etc. that may be applicable to multiple pages?
  1. Is recommended practice to include all defines from a core defines file in a override file or just the defines being overridden?
  1. How should current array defines be handled, including array defines that are associative arrays? (Ex: define('CONSTANT_NAME', array(key1=>value1, key2=>value2));

Thank you!
Dave
zc1.5.8a, php 8.2, mysql 8.0, only one language needed - English
While the link that @swguy provides a good overview, I'll try to "fill in the blanks" with some specifics.

  1. where is the recommended place and filename for overriding core defines?
  2. For core base language files, e.g. /includes/languages/lang.english.php, place the override in /includes/languages/YOUR_TEMPLATE/lang.english.php
  3. For page-specific core language files, e.g. /includes/languages/english/lang.address_book_process.php, place the override in /includes/languages/english/YOUR_TEMPLATE/lang.address_book_process.php
  4. Customer defines applicable to a specific page which don't override 'core' definitions
  5. Either /includes/languages/english/lang.specific_page_suffix.php (if the additions apply to all templates) or /includes/languages/english/YOUR_TEMPLATE/lang.specific_page_suffix.php (if the additions apply only to a specific template).
  6. Where is the recommended place and filename for defines needed for functions, observers, etc. that may be applicable to multiple pages?
  7. /includes/languages/english/extra_definitions/lang.my_extra_definitions.php
  8. hen overridding a core file, I suggest just including the definitions that are being overridden; it makes an upgrade that much easier.
  9. For this one, I'd need an example, since there's no such usage in the core, for verification. My initial take would be to use
$define = [
   'CONSTANT_NAME' => [
       'key1' => 'value1',
       'key2' => 'value2',
   ],
];
13 Oct 2024, 1:58 PM
#4
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,974
Plugin Contributions:
8

Re: Where and how on new define approach

lat9:

[*]For this one, I'd need an example, since there's no such usage in the core, for verification. My initial take would be to use
[/LIST]

$define = [
'CONSTANT_NAME' => [
'key1' => 'value1',
'key2' => 'value2',
],
];


uh....  do not forget the missing last line:

```php
$define = [
   ...
    ],
];
return $define;

given the fact that the return is the important part, one can name the var as one sees fit. ie:

$myLanguageOverrides = [
   ...
    ],
];
return $myLanguageOverrides;

forgetting to return your overrides is a very common mistake. and will result in an error.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

13 Oct 2024, 2:50 PM
#5
lat9 avatar

lat9

Administrator

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

Re: Where and how on new define approach

Thanks, @carlwhat, for that correction!

13 Oct 2024, 3:29 PM
#6
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Where and how on new define approach

Thanks to all of you for your very helpful answers!
Dave

20 Oct 2024, 6:28 PM
#7
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Where and how on new define approach

Unfortunately, the answer for question 5, how to handle defines which are arrays, does not work. Fatal error logs follow:

[20-Oct-2024 13:50:11 America/New_York] PHP Fatal error: Uncaught TypeError: preg_match_all(): Argument #2 ($subject) must be of type string, array given in /my_domain/includes/classes/ResourceLoaders/ArraysLanguageLoader.php:25
Stack trace:
#0 /my_domain/includes/classes/ResourceLoaders/ArraysLanguageLoader.php(25): preg_match_all('/%{2}([^%]+)%{2...', Array, Array, 1)
#1 /my_domain/includes/classes/ResourceLoaders/LanguageLoader.php(34): Zencart\LanguageLoader\ArraysLanguageLoader->makeConstants(Array)
#2 /my_domain/includes/init_includes/init_templates.php(88): Zencart\LanguageLoader\LanguageLoader->finalizeLanguageDefines()
#3 /my_domain/includes/autoload_func.php(40): require_once('/...')
#4 /my_domain/includes/application_top.php(237): require('/...')
#5 /my_domain/index.php(25): require('/...')
#6 {main}
thrown in /my_domain/includes/classes/ResourceLoaders/ArraysLanguageLoader.php on line 25

[20-Oct-2024 13:50:11 America/New_York] Request URI: /, IP address: ::1
--> PHP Fatal error: Uncaught TypeError: preg_match_all(): Argument #2 ($subject) must be of type string, array given in /my_domain/includes/classes/ResourceLoaders/ArraysLanguageLoader.php:25
Stack trace:
#0 /my_domain/includes/classes/ResourceLoaders/ArraysLanguageLoader.php(25): preg_match_all('/%{2}([^%]+)%{2...', Array, Array, 1)
#1 /my_domain/includes/classes/ResourceLoaders/LanguageLoader.php(34): Zencart\LanguageLoader\ArraysLanguageLoader->makeConstants(Array)
#2 /my_domain/includes/init_includes/init_templates.php(88): Zencart\LanguageLoader\LanguageLoader->finalizeLanguageDefines()
#3 /my_domain/includes/autoload_func.php(40): require_once('...')
#4 /my_domain/includes/application_top.php(237): require('/...')
#5 /my_domain/index.php(25): require('/...')
#6 {main}
thrown in /my_domain/includes/classes/ResourceLoaders/ArraysLanguageLoader.php on line 25.

The code line in the midst of other defines is:

'SHIRT_PIDS' => [0 => 706, 1 => 757,],

Commenting out the above line resulted in no errors. Given that I have used arrays in a number of defines ever since defines would accept arrays, I really need a solution that works for the new defines approach. Any suggestions?

I'm assuming that the $define array element values can't be arrays, so at least in the present case, I might be able to set SHIRT_PIDS_0 to 706, and SHIRT_PIDS_1 to 757, and construct an array later in the code from the constants. Ugh!:censored:

Or would 'SHIRT_PIDS' => '[0 => 706, 1 => 757,]', work, since the code wants a string???

Thank you!

Dave
zc158a, php 8.2, mysql 8.0, bootstrap template, OPC, SBA, ZX POS, other custom code

20 Oct 2024, 6:46 PM
#8
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Where and how on new define approach

Or maybe the following:

'SHIRT_PIDS[0]' => 706,
'SHIRT_PIDS[1]' => 757,

The above gets through the defining process without errors. Will check to see if the rest of code works.
Dave

21 Oct 2024, 9:18 AM
#9
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,747
Plugin Contributions:
56

Re: Where and how on new define approach

These are not language defines. Just put them in includes/extra_configures and use the native PHP define syntax.
Or if you need to tweak them periodically, put them in the database as a comma separated list.

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

24 Oct 2024, 11:49 PM
#10
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Where and how on new define approach

Thanks swguy. That helps a lot.

Follow-on question for language files in the admin. Does substring matching referred to in https://docs.zen-cart.com/dev/languages/158_order_language_files/ work in the admin? For example, will admin/includes/languages/english/lang.customers_my_custom_defines.php get loaded since it is a substring match to .../lang.customers.php?

Dave

4 Aug 2025, 2:59 PM
#11
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Where and how on new define approach

Where's the best place to override defines in an encapsulated plugin? Thanks!
Dave
zc2.1.0, bootstrap, opc, edit orders, clone a template

4 Aug 2025, 4:55 PM
#12
lat9 avatar

lat9

Administrator

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

Re: Where and how on new define approach

Dave224:

Thanks swguy. That helps a lot.

Follow-on question for language files in the admin. Does substring matching referred to in https://docs.zen-cart.com/dev/languages/158_order_language_files/ work in the admin? For example, will admin/includes/languages/english/lang.customers_my_custom_defines.php get loaded since it is a substring match to .../lang.customers.php?

Dave
See this docs' page: https://docs.zen-cart.com/dev/admin/templating/

4 Aug 2025, 5:00 PM
#13
lat9 avatar

lat9

Administrator

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

Re: Where and how on new define approach

Dave224:

Where's the best place to override defines in an encapsulated plugin? Thanks!
Dave
zc2.1.0, bootstrap, opc, edit orders, clone a template
I'm "assuming" that you're talking about language-defines. For a detailed dive into the language-constant loading precedence, see this link: https://docs.zen-cart.com/dev/languages/language_constant_logic/

21 Dec 2025, 5:02 PM
#14
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Where and how on new define approach

lat9:

I'm "assuming" that you're talking about language-defines. For a detailed dive into the language-constant loading precedence, see this link: https://docs.zen-cart.com/dev/languages/language_constant_logic/

Still struggling with language defines. Where should overrides to defines in /zc_plugins/EditOrders/v5.0.2/catalog/includes/languages/english/modules/order_total/lang.ot_misc_cost.php be located. I tried placing the override in /includes/languages/english/extra_definitions/lang.ot_misc_cost.php and in /includes/languages/english/extra_definitions/modules/order_total/lang.ot_misc_cost.php based on my read of the above link. Neither worked. I finally edited the source file which worked, but I'd rather not touch the plugin. Suggestions?
Thank you.
Dave
zc2.1.0, php 8.3, mysql 8.0, edit orders 5.0.2, bootstrap 3.7.8, OPC 2.5.5, PayPal Restful 1.3.0, Square WebPay