【问题标题】:Prefill the billing fields in woocommerce checkout [duplicate]预填充 woocommerce 结帐中的计费字段 [重复]
【发布时间】:2017-06-23 13:04:02
【问题描述】:

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

而客户希望订单流程是这样的

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

所以问题是我如何将这些数据传递给结帐?

另外,如何在填写传递的数据之前清除这些字段?

【问题讨论】:

    标签: wordpress woocommerce checkout


    【解决方案1】:

    这个解决方案帮助了我 - 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);
    

    【讨论】:

    • 如何预填充选择字段?
    【解决方案2】:

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

    $woocommerce->session->set('survey_email', $_POST['survey_email']);
    

    然后,我将连接到 woocommerce_checkout_fields 过滤器 (see docs) 以填写表单字段。您不能直接设置该字段的值,但您可以设置默认值,如果用户登录,则可以将其覆盖。

    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' );
    

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 2019-10-15
      • 2018-12-25
      • 2018-10-25
      • 1970-01-01
      • 2021-06-12
      • 2018-03-09
      • 1970-01-01
      • 2019-05-27
      相关资源
      最近更新 更多