在 woocommerce checkout 中预填帐单字段

Ole*_*leg 2 wordpress checkout woocommerce

在我的项目中,我使用 woocommerce 的直接结账插件,以便在一个页面上进行购物车和结账。

而客户想要的订单流程是这样的

  1. 我们在 wordpress 中有一个订单页面,用户可以在其中输入姓名、地址、电话、电子邮件,选择价格,然后重定向到直接结账。
  2. 在结帐页面上,我们希望字段 billing_name、billing_address 等已经预先填充了来自订单页面的数据。

所以问题是我怎么能把这些数据传递给结账?

另一个如何在填充传递的数据之前清除这些字段?

小智 6

我刚刚在我正在构建的网站上遇到了同样的问题。我们正在调查中收集电子邮件地址,如果用户继续结账,我们希望预先填写电子邮件字段。当提交调查表单时,我将电子邮件地址存储在 WooCommerce 会话中,如下所示:

$woocommerce->session->set('survey_email', $_POST['survey_email']);
Run Code Online (Sandbox Code Playgroud)

然后,我连接到woocommerce_checkout_fields过滤器(请参阅文档)来填充表单字段。您不能直接设置该字段的值,但可以设置默认值,这样在用户登录时该值就会被覆盖。

function override_checkout_email_field( $fields ) {
    global $woocommerce;
    $survey_email = $woocommerce->session->get('survey_email');
    if(!is_null($survey_email)) {
      $fields['billing']['billing_email']['default'] = $survey_email;
    }
    return $fields;
}

add_filter( 'woocommerce_checkout_fields' , 'override_checkout_email_field' );
Run Code Online (Sandbox Code Playgroud)


ant*_*kiy 6

这个解决方案帮助了我 - https://www.bobz.co/pre-populate-woocommerce-checkout-fields/

<?php
/**
 * Pre-populate Woocommerce checkout fields
 */
add_filter('woocommerce_checkout_get_value', function($input, $key ) {
    global $current_user;
    switch ($key) :
        case 'billing_first_name':
        case 'shipping_first_name':
            return $current_user->first_name;
        break;

        case 'billing_last_name':
        case 'shipping_last_name':
            return $current_user->last_name;
        break;
        case 'billing_email':
            return $current_user->user_email;
        break;
        case 'billing_phone':
            return $current_user->phone;
        break;
    endswitch;
}, 10, 2);
Run Code Online (Sandbox Code Playgroud)