在Woocommerce中使用CRUD对象设置客户用户数据 [英] Setting customer user data with CRUD object in Woocommerce

查看:105
本文介绍了在Woocommerce中使用CRUD对象设置客户用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为woocommerce上的用户设置一个first_name的新值,我想使用woocommerce 3.0上一文档中的 CRUD Objects in 3.0来实现此目的。

I'm trying to set a new value for the first_name for a user on woocommerce, i want to do this using the 'CRUD Objects in 3.0' present in the last documentation of woocommerce 3.0.

Só我定义变量:

$ wc_current_user = WC()-> customer;

$wc_current_user = WC()->customer;

这将返回WC_Customer类的实例,该实例具有$ data数组,其中包含有关客户的数据,例如$ first_name,$ last_name,$ email,$ billing_address数组等。 ...

This return an instance of the class WC_Customer with has an array of $data with data about customer like $first_name, $last_name, $email, $billing_address array and so on...

我正在尝试重写edit-account.php表单,并希望将此数据提交到此对象上,他提供了getter和setter方法来执行此操作,但似乎二传手却没有用,他没有说数据。

i'm trying to rewrite the edit-account.php form and want to submit this data on this object, he provide the getters and setters to do this, but seems the setter ins't working, he is not saiving the data.

我正在这样做:

我有一个使用用户名字的表格,这很好用,我正在使用-> get_first_name并且很好用。

I have a form with takes the first name from the user, this is working fine, i'm using the ->get_first_name and is working fine.

  <form class="woocommerce-account-information" action="" method="post">

    <label>First Name</label>
     <input type="text" name="first_name" value="<?php echo 
     $wc_current_user->get_first_name()?>"/>

     <button type="submit">SAVE</button>
  </form>

问题我在这里,当我尝试使用setter提交此数据时'object-> set_first_name'和'object-> save()',什么都没发生,有人可以帮助我吗?

The Problema is here, when i try to submit this data using a setter, which in this case is 'object -> set_first_name' and 'object->save()', nothing happens, someone can help me?

这是我的代码:

  if( isset($_POST['first_name'] ) ){
     $wc_current_user->set_first_name($_POST['first_name']);
     $wc_current_user->save();

   }

//这不行,你知道吗怎么了?

//THIS ^ DON'T WORK, do you know what is wrong?

我有兴趣了解旧方法和新方法,如果有人可以帮助我,那将是很大的帮助。谢谢!

I'm interested in knowing both the old and the new method, if anyone can help me, it will be a great help. Thank you!

推荐答案

我找到了解决此问题的方法,当您使用这样的对象时,需要使用$ object-内森(Nathan)所说的save()方法加上$ object-> apply_changes();提交替换数据库中的数据:

I find a solution for this problem, when you use object like this you need to use the $object-save() method that Nathan has said plusthe $object->apply_changes(); to submit replace the data on data-base:

public function apply_changes() {
    $this->data    = array_replace_recursive( $this->data, $this->changes );
    $this->changes = array();
}

我的工作代码如下:

       $wc_current_user = WC()->customer;

       if ( isset($_POST['first_name']) ) {
            $wc_current_user->set_first_name($_POST['first_name']);
            $wc_current_user->save();
            $wc_current_user->apply_changes();
      }

这篇关于在Woocommerce中使用CRUD对象设置客户用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆