在 Woocommerce 中调用 woocommerce_customer_save_address 时出现致命错误

问题描述 投票:0回答:1

希望能帮到你。我遇到一个奇怪的问题,不知道如何解决。我熟悉调用操作,但在调用空的标准 woo hook 时遇到致命错误。

我正在“我的帐户”的地址部分致电 woocommerce_customer_save_address。我为“公司名称”创建了一个填充良好的字段。但是,如果我更改它并运行操作来添加此字段,则会收到错误: “致命错误:未捕获的 ArgumentCountError:函数 save_address_changes() 的参数太少,传入了 2 个” 这是代码:

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
}
}

我开始抽出我正在做的事情,最终抽出了该函数所做的所有事情。所以它没有任何作用。但仍然出错。我看到错误表明它只获得了 3 个参数中的 2 个。我认为列出的这些参数是由 woo 和更大的操作定义的全局参数。我不认为这些是我应该设置的。是这样吗?

您有任何想法如何解决此问题并弄清楚发生了什么吗?似乎当保存地址挂钩被触发时,它会执行一系列其他操作,然后使用已知参数运行我的代码片段,因为这些似乎是此挂钩中应包含的标准。

无论如何,您能提供的任何帮助都会很棒。谢谢!

user-interface woocommerce save hook-woocommerce fatal-error
1个回答
0
投票

woocommerce_customer_save_address
操作钩子只有2个参数
$user_id
$address_type
),所以要解决这个问题,请使用以下代码:

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
    }
}

现在应该可以工作了。

© www.soinside.com 2019 - 2024. All rights reserved.