i’m having issue after some woocommerce ( v. 8.1.1 ) and wordpress ( v. 6.3.1 ) updates to last versions. Problem is on woocommerce checkout page, i created a class file some time ago "class-checkout-page.php" and all worked correctly, but i recently run in a php fatal error as below
PHP Fatal error: Uncaught Error: Call to a member function add_error() on array in /...../public_html/wp-content/themes/..../modules/checkout-page/class-checkout-page.php:130
Stack trace:
#0 /...../public_html/wp-includes/class-wp-hook.php(312): Checkout->validate_extra_fields(Array)
#1 /...../public_html/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters(NULL, Array)
#2 /...../public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#3 /...../public_html/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(957): do_action('woocommerce_aft...', Array, Object(WP_Error))
#4 /...../public_html/wp-content/plugins/woocommerce/includes/class-wc-checkout.php(1245): WC_Checkout->validate_checkout(Array, Object(WP_Error))
#5 /...../public_html/wp-content/plugins/woocommerce/includes/class-wc-ajax.php(508): WC_Checkout->process_checkout()
#6 /...../public_html/wp-includes/class-wp-hook.php(310): WC_AJAX::checkout('')
#7 /...../public_html/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters('', Array)
#8 /...../public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#9 /...../public_html/wp-content/plugins/woocommerce/includes/class-wc-ajax.php(96): do_action('wc_ajax_checkou...')
#10 /...../public_html/wp-includes/class-wp-hook.php(310): WC_AJAX::do_wc_ajax('')
#11 /...../public_html/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters(false, Array)
#12 /...../public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#13 /...../public_html/wp-includes/template-loader.php(13): do_action('template_redire...')
#14 /...../public_html/wp-blog-header.php(19): require_once('/home2/...')
#15 /...../public_html/index.php(17): require('/home2/...')
#16 {main}
thrown in /...../wp-content/themes/..../modules/checkout-page/class-checkout-page.php on line 130
this is block function that include line 130 in that file class-checkout-page.php
/**
* Validate the extra fields on the checkout page.
*
* @param $fields The validation object.
* @return WC_Validation The modified validation object.
*/
public function validate_extra_fields($fields)
{
if (empty($_POST['shipping_email'])) {
$fields->add_error('shipping_email', 'Plese insert your email address');
} elseif (!is_email($_POST['shipping_email'])) {
$fields->add_error('shipping_email', 'Email format is invalid');
}
if (empty($_POST['shipping_phone'])) {
$fields->add_error('shipping_phone', 'Please insert your phone number');
}
return $fields;
}
I’m not finding solution for it, where could be problem and possible solution ?
thanks for any helps.
2
Answers
than you for your reply, i added an answer to my post to correctly give more details.
In the same file class-checkout-page.php i declared $fields to add_fields for shipping_fields as below
In my functions.php file there's nothing that decleares $fields. I tested on wocoommerce frontend as customer, if i compile shipping_fields i haven't problem to complete checkout and order, so i think that something was changes in new woocommerce version, and not for add_error(), cause i tried use even wc_add_notice() without any result.
Maybe i need to create a conditions if woocommerce-shipping-fields checkbox isn't checked or shipping_fields are empty there must be no control for validation fields? However in actual way worked some weeks ago.
Any suggestions ? Thanks
From looking at the code, it is erroring on the $fields parameter being sent into the function.
Uncaught Error: Call to a member function add_error() on array
The error is suggesting that the $fields parameter is attempting to call add_error but the $fields parameter, in this case, is an array and doesn’t have the function attached to it.
I would start by looking at the data you are passing into the function and confirm that you are passing the correct variable type in.
If you are using a hook, such as in your functions.php file, it might be useful for us to see that also as this is probably where the data is being passed in.
Hope this helps to narrow down the issue for you.