phpwoocommercehook-woocommercefatal-erroruser-data

Fatal Error when calling woocommerce_customer_save_address action hook


I hope you can help. I am getting a strange issue and not sure how to solve it. I'm familiar with calling actions, but I am getting a fatal error when calling a standard woo hook that is empty.

I'm calling woocommerce_customer_save_address on the addresses section of the My Account. I've created a field for Company Name which populates nicely. But if I change it and run an action to add this field I get the following error: "Fatal error: Uncaught ArgumentCountError: Too few arguments to function save_address_changes(), 2 passed in"

Here is the code:

add_action('woocommerce_customer_save_address', 'save_address_changes', 10, 3);

function save_address_changes($user_id, $load_address, $address_type) {
    // Check if the address type is billing or shipping
    if ($address_type === 'billing') {
        // Handle address changes here
    }
}

I started pulling out what I was doing and ended up pulling out everything the function does. So it doesn't do anything. But still errors. I see the error says that it only got 2 of the 3 arguments. I thought these parameters listed are globals defined by woo and the larger action. I didn't think these were things I should be setting. Is that true?

Do you have any ideas how to troubleshoot this and figure out what is happening? It seems like when the save address hook is fired it does a bunch of other stuff then runs my snippet with known parameters as these seem to be the standard of what should be in this hook.


Solution

  • The woocommerce_customer_save_address action hook has only 2 arguments ($user_id and $address_type), so to solve this issue, use the following instead:

    add_action( 'woocommerce_customer_save_address', 'customer_save_address_changes', 10, 2 );
    function customer_save_address_changes( $user_id, $address_type ) {
        // Check if the address type is billing or shipping
        if ($address_type === 'billing') {
            // Handle address changes here
        }
    }
    

    It should work now.