I’m currently writing a WordPress plugin that works with WooCommerce. I query WooCommerce orders and the goal is to use the adresses of those orders to plan a delivery route using the API from a Dutch route planner service.
It’s been a while writing PHP for me, the last PHP I wrote was 7 years ago, so my PHP skills are a bit rusty.
After I query my orders, I want to call the function routexl_generate(), this function is defined in my code. However, running code this throws the following error:
Fatal error: Uncaught Error: Call to undefined function routexl_generate() in /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-content/plugins/geusdiervoeding-wc-rxlplanner/geusdiervoeding-wc-rxlplanner.php:120 Stack trace: #0 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-content/plugins/geusdiervoeding-wc-rxlplanner/geusdiervoeding-wc-rxlplanner.php(100): wc_order_query() #1 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-includes/class-wp-hook.php(288): rxlp_menu_init(”) #2 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-includes/class-wp-hook.php(312): WP_Hook->apply_filters(”, Array) #3 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #4 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-admin/admin.php(254): do_action(‘woocommerce_pag…’) #5 {main} thrown in /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-content/plugins/geusdiervoeding-wc-rxlplanner/geusdiervoeding-wc-rxlplanner.php on line 120
I don’t really see why it behaves like that, since the function is defined… Help would be appreciated.
This is (part of) my code:
function wc_order_query()
{
if(isset($_GET['area'])) {
$route = $_GET[ 'area' ];
$date = $_GET[ 'date' ];
$args = array(
'status' => $route,
'meta_key' => 'jckwds_date',
'meta_value' => $date,
);
$orders = wc_get_orders( $args );
if (empty($orders)) {
echo ('<ul><li>Op '.$date.' zijn er geen bezorgorders voor het gekozen routegebied.</li></ul>');
} else {
routexl_generate();
}
//RouteXL API aanroepen
function routexl_generate(){
//Query results to API
}
}
3
Answers
First you have to declare function then call you made a mistake you are calling a function before declaration
I realise this is ‘only part of your code’, but there are issues with what you’ve shown:
wc_order_query()
function isn’t closed;routexl_generate()
is insidewc_order_query()
;routexl_generate()
is inside anif
conditional insidewc_order_query()
Technically there are no problems with nested functions, but for the sake of clarity it would make sense to move
routexl_generate()
outside of the function entirely:Tested & works.
If you define a function INSIDE AN IF then it is not actually defined until it is reached by an actual execution. In other words it is not define in the normal code interpretation process as functions that are defined OUTSIDE an IF would be
So if you want to define functions inside IF’s and I would recommend against it, you have to define them before you call them as if the interpreter were only single pass.
Example: This works
RESULT
And this does not
RESULT