在WooCommerce中以编程方式更新客户的帐单信息

Dav*_*ini 7 php account wordpress billing woocommerce

我有一个表单,用户可以注册一个事件,如果他们愿意,可以随时更新一些帐单信息。

我有他们可以更新的信息列表,例如

 $inputs = array(
        'billing_city'          => 'City',
        'billing_postcode'      => 'Postcode',
        'billing_email'         =>  'Email',
        'billing_phone'         =>  'Phone',
    );
Run Code Online (Sandbox Code Playgroud)

然后,我尝试使用WC_Customer该类来更新更改的信息:

$customer = new WC_Customer( get_current_user_id() );
foreach ($inputs as $key => $label ) {
     $method = 'set_'. $key;
     $customer->$method( $value );
}
Run Code Online (Sandbox Code Playgroud)

这似乎很简单。但是,帐单信息不会更改。

我究竟做错了什么?还有其他功能可以解决此问题吗?

Woocommerce文档并没有真正解释太多。

Loi*_*tec 4

您可以使用函数来完成update_user_meta()此操作,如下所示:

$user_id =  get_current_user_id();

$data = array(
    'billing_city'          => $city_value,
    'billing_postcode'      => $postcode_value,
    'billing_email'         => $email_value,
    'billing_phone'         => $phone_value,
);
foreach ($data as $meta_key => $meta_value ) {
    update_user_meta( $user_id, $meta_key, $meta_value );
}
Run Code Online (Sandbox Code Playgroud)

您需要设置数组中的值。