Woocommerce:设置结帐字段值

Dif*_*ter 5 php wordpress field checkout woocommerce

对于我系统中还不是WP用户的特殊客户群,我将他们引导到一个特殊页面,他们将从一组有限的产品中进行选择.我已经拥有了他们所有的信息,我将在此着陆页上预先填充.当他们验证了他们的信息后,它会将他们的产品添加到购物车并直接跳到结账处.到目前为止,我已经完成了这一切.

我想要做的是使用我拥有的客户名称和账单信息预先填写结账数据,我不完全确定如何做到这一点.但这是我到目前为止所得到的:

    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' );
Run Code Online (Sandbox Code Playgroud)

如何更改结帐字段的值?上面的代码没有这样做.把我指向一个正确的方向,我可以做其余的事情.

我看了看这里,但也找不到我要找的东西.

谢谢!

Loi*_*tec 6

您应该使用这个专用的WooCommerce挂钩,该挂钩是为预填充结帐字段而创建的,并在WC_Checkout方法中定义get_value()

add_filter( 'woocommerce_checkout_get_value', 'populating_checkout_fields', 10, 2 );
function populating_checkout_fields ( $value, $input ) {

    $token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';

    if( 'testtoken' == $token ) {
        // Define your checkout fields  values below in this array (keep the ones you need)
        $checkout_fields = array(
            'billing_first_name'    => 'John',
            'billing_last_name'     => 'Wick',
            'billing_company'       => 'Murders & co',
            'billing_country'       => 'US',
            'billing_address_1'     => '7 Random street',
            'billing_address_2'     => 'Royal suite',
            'billing_city'          => 'Los Angeles',
            'billing_state'         => 'CA',
            'billing_postcode'      => '90102',
            'billing_phone'         => '555 702 666',
            'billing_email'         => 'jhon.wick@murders.com',
            'shipping_first_name'   => 'John',
            'shipping_last_name'    => 'Wick',
            'shipping_company'      => 'Murders & co',
            'shipping_country'      => 'USA',
            'shipping_address_1'    => '7 Random street',
            'shipping_address_2'    => 'Royal suite',
            'shipping_city'         => 'Los Angeles',
            'shipping_state'        => 'California',
            'shipping_postcode'     => '90102',
            // 'account_password'       => '',
            'order_comments'        => 'This is not for me',
        );
        foreach( $checkout_fields as $key_field => $field_value ){
            if( $input == $key_field && ! empty( $field_value ) ){
                $value = $field_value;
            }
        }
    }
    return $value;
}
Run Code Online (Sandbox Code Playgroud)

代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

如果用户未登录,则可以在您的条件中另外添加:

 if( 'testtoken' == $token &&  ! is_user_logged_in() ) {
Run Code Online (Sandbox Code Playgroud)

此代码已经过测试并且可以工作(但未在您的特定代码条件下进行过测试)。对于我的测试,我已将其! is_user_logged_in()作为条件。

您将获得此信息(用于函数中定义的数组):

在此处输入图片说明


小智 6

请使用“默认”,例如:

$fields['billing']['billing_first_name']['default'] = "Thomas";
Run Code Online (Sandbox Code Playgroud)

请参阅WooCommerce | 设置账单字段值


ran*_*ame 5

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

所以,在这种情况下,你只是return $fields;在函数中缺少一个:

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' );
Run Code Online (Sandbox Code Playgroud)

更新:
看到由于某些原因,上述方法无效后,我闻了闻上另一个插件一些代码,和他们的方式,他们这样做(和它显然作品)是像这样:

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;
}
Run Code Online (Sandbox Code Playgroud)

所以 - 为了肯定这不会覆盖/踩踏用户输入的信息,我可能会建议考虑做这样的事情(当然测试以确保它有效):

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;
}
Run Code Online (Sandbox Code Playgroud)