Problem with call to a member function add_error() Wocoommerce

81 Views Asked by At

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.

3

There are 3 best solutions below

1
Mike Pengelly On

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.

0
Zarbat On

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

 public function add_extra_fields_to_shipping($fields)
    {

        $fields['shipping_phone'] = array(
            'label'       => 'Phone',
            'required'    => true,
            'type'        => 'tel',
            'class'       => array('form-row-wide'),
            'placeholder' => '',
        );

        $fields['shipping_email'] = array(
            'label'       => 'Email address',
            'required'    => true,
            'type'        => 'email',
            'class'       => array('form-row-wide'),
            'placeholder' => '',
        );

        return $fields;
    }

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

0
Zarbat On

I have updated code adding a if condition as below

/**
 * 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 ( $woocommerce_ship_to_different_address_checked == 1){

    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');
      }
}
         
        else {
            //do nothing
        }
        return $fields;
    }
?>

And it seems works having a php warning as

PHP Warning: Undefined variable $woocommerce_ship_to_different_address_checked in /..../public_html/wp-content/themes/...../modules/checkout-page/class-checkout-page.php on line 130

and obv in line 130 i have

if ( $woocommerce_ship_to_different_address_checked == 1){

Some suggestions if this solution is nearly correct and how to correct completely it?

Thanks