在WooCommerce中以编程方式更新客户的结算信息

时间:2017-08-29 13:39:39

标签: php wordpress woocommerce account billing

我有一个用户注册活动的表单,如果他们愿意,他们可以动态更新部分结算信息。

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

 $inputs = array(
        'billing_city'          => 'City',
        'billing_postcode'      => 'Postcode',
        'billing_email'         =>  'Email',
        'billing_phone'         =>  'Phone',
    );

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

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

看起来很简单。但是,计费信息不会更改。

我做错了什么?是否有其他功能可以解决这个问题?

Woocommerce文档并没有解释得太多。

2 个答案:

答案 0 :(得分:3)

您可以使用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 );
}

您需要在数组中设置值。

答案 1 :(得分:0)

您必须在设置属性后保存更改。在代码中,在foreach之后,添加:

$customer->save();

瞧!