Woocommerce:设置结帐字段值 [英] Woocommerce: Set checkout field values

查看:78
本文介绍了Woocommerce:设置结帐字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在我的系统中尚未成为WP用户的特殊客户群,我将他们定向到一个特殊页面,他们将从有限的一组产品中进行选择。我已经掌握了他们的所有信息,并将其预先填充在此着陆页上。他们验证信息后,会将其产品添加到购物车,然后直接跳至结帐。到目前为止,我已经掌握了所有这些。



我想做的是用我拥有的客户名称和帐单信息预先填充结帐数据,但我不确定如何做到这一点。但是到目前为止,这是我得到的:

 函数onboarding_update_fields($ fields = array()){

$ token =(!empty($ _GET ['token']))? $ _GET ['token']:’;
if(‘testtoken’== $ token){
$ fields [’billing’] [’billing_first_name’] [’value’] =‘Joe’;
var_dump($ fields);
}
返回$ fields;
}

add_filter(‘woocommerce_checkout_fields’,‘onboarding_update_fields’);

如何更改结帐字段的值?上面的代码无法做到。在一个正确的方向上指出我,其余的我可以做。



我看着


谢谢!

过滤器允许您修改信息,但是您必须从函数中返回该信息。



因此,在这种情况下,您只是错过了 return $字段; 您的函数中:

  function onboarding_update_fields($ fields = array()){
//检查是否设置为防止引发通知
$ token =(!empty($ _GET ['token']))吗? $ _GET ['token']:’;

// yoda样式以防止意外分配
if('testtoken'== $ token){
//如果遇到问题,在下面执行此操作很有用:
var_dump($ fields);
//工作正常后删除var_dump

//如果您要更改的只是值,则仅分配值
$ fields ['billing '] ['billing_first_name'] ['value'] ='Joe';
//删除数组的核心/必需部分之前的操作方式-请勿这样做。
// $ fields ['billing'] ['billing_first_name'] ['value'] = array(‘value’=>‘Joe’);

}
//必须返回字段数组
return $ fields;
}

add_filter(‘woocommerce_checkout_fields’,‘onboarding_update_fields’);

更新:

上面的方法不起作用,我在另一个插件上嗅了一些代码,他们他们做到了(显然起作用)是这样的:

  function onboarding_update_fields($ fields = array()){
$ token =(!empty($ _GET ['token'])))吗? $ _GET ['token']:’;

if(‘testtoken’== $ token){
//将值分配给$ _POST超全局
$ _POST [’billing_first_name’] =‘Joe’;
}

返回$ fields;
}

所以-可以肯定的是,这不会覆盖/踩踏用户输入的内容信息,我可能建议考虑这样做(当然要进行测试,以确保它可以正常工作):

  function onboarding_update_fields($字段= array()){
$ token =(!empty($ _GET ['token']))? $ _GET ['token']:’;

if('testtoken'== $ token){
//仅将值分配给$ _POST超全局变量,如果尚未设置
if(empty($ POST [' billing_first_name'])){
$ _POST ['billing_first_name'] ='Joe';
}
}

return $ fields;
}


For a special group of customers that aren't yet WP user in my system, I am directing them to a special page, where they'll choose from a limited set of products. I already have all of their information and I it's going to pre-populate on this landing page. When they've verified their information, it will add their product to the cart and skip straight to the checkout. I've got all that down so far.

What I want to do is pre-populate the checkout data with the customer name and billing information that I have and I'm not entirely certain how to do that. But here's what I've got so far:

    function onboarding_update_fields( $fields = array() ) {

      $token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';   
      if( 'testtoken' == $token ) {          
        $fields['billing']['billing_first_name']['value'] = 'Joe';
        var_dump( $fields ); 
      }  
      return $fields;
     }

    add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );

How do I change the value of the checkout fields? The code above doesn't do it. Point me in the right direction on one, and I can do the rest.

I looked here but didn't quite find what I was looking for either.

Thanks!

解决方案

Filters allow you to modify information, but you must return that information from your function.

So, in this case, you're simply missing a return $fields; in your function:

function onboarding_update_fields( $fields = array() ) {
   // check if it's set to prevent notices being thrown
   $token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';

   // yoda-style to prevent accidental assignment
   if( 'testtoken' == $token ) {
       // if you are having issues, it's useful to do this below:
       var_dump( $fields );
       // remove the var_dump once you've got things working

       // if all you want to change is the value, then assign ONLY the value
       $fields['billing']['billing_first_name']['value'] = 'Joe';
       // the way you were doing it before was removing core / required parts of the array - do not do it this way.
       // $fields['billing']['billing_first_name']['value'] = array( 'value' => 'Joe');

   }
   // you must return the fields array 
   return $fields;
}

add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );

Update:
After seeing for some reason the above doesn't work, I sniffed some code on another plugin, and they way they do it (and it clearly works) is like so:

function onboarding_update_fields( $fields = array() ) {
   $token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';

   if( 'testtoken' == $token ) {
       // Assign the value to the $_POST superglobal
       $_POST['billing_first_name'] = 'Joe';
   }

   return $fields;
}

So - to be positive that this didn't overwrite / stomp user-entered information, I might suggest considering doing it something like this (of course test to be sure it works):

function onboarding_update_fields( $fields = array() ) {
   $token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';

   if( 'testtoken' == $token ) {
       // Assign the value to the $_POST superglobal ONLY if not already set
       if ( empty( $POST['billing_first_name'] ) ) {
           $_POST['billing_first_name'] = 'Joe';
       }
   }

   return $fields;
}

这篇关于Woocommerce:设置结帐字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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