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

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

问题描述

我正在尝试为 woocommerce 上的用户的 first_name 设置一个新值,我想使用 woocommerce 3.0 的最后一个文档中存在的3.0 中的 CRUD 对象"来执行此操作.

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.

所以我正在定义变量:

$wc_current_user = WC()->客户;

$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 来执行此操作,但似乎 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!

推荐答案

我找到了解决这个问题的方法,当你像这样使用对象时,你需要使用 Nathan 所说的 $object-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();
}

我的工作代码如下所示:

My working code looks like this:

       $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天全站免登陆