【问题标题】:Pre populate Woocommerce checkout fields预填充 Woocommerce 结帐字段
【发布时间】:2019-08-24 23:14:14
【问题描述】:

我正在尝试在 woocommerce 结帐页面中预填充其他字段,但我正在为此苦苦挣扎。

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;

        case 'billing_phone':
            return $current_user->phone;
        break;
                case 'billing_company':
                case 'shipping_company':
            return $current_user->company;
        break;
                case 'billing_vat':
            return $current_user->vat;
        break;
    endswitch;
}, 10, 2);

它适用于 $current_user->phone、$current_user->company、$current_user->vat

有什么帮助吗?

【问题讨论】:

    标签: php wordpress woocommerce checkout hook-woocommerce


    【解决方案1】:

    电话、公司和其他信息在元数据中。

     $phone = get_user_meta($current_user,'phone_number',true);
    

    您也不需要有一个全局变量。这也是危险的。

     add_filter('woocommerce_checkout_get_value', function($input, $key ) 
        {
         $current_user = get_current_user_id();
    
        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;
    
        case 'billing_phone':
            $phone = get_user_meta($current_user,'phone_number',true);
            return  $phone;
        break;
    
        case 'billing_company':
        case 'shipping_company':
             // May or may not be in meta data
        break;
    
       case 'billing_vat':
           // Not available through user
        break;
       endswitch;
       }, 10, 2);
    

    您可以在此处查看更多信息: https://codex.wordpress.org/Function_Reference/get_user_meta

    增值税有点复杂,因为它基于国家而不是用户名。虽然国家存在,但增值税不会存在。检索它的最佳方法是通过 woocommerce。

    至于公司名称(有时称为组织)也不是直接的 Wordpress。它通常通过 3rd 方插件添加,例如 woocommerce、会员或自定义插件,该插件会将功能添加到帐户。你必须看看你在用什么。

    【讨论】:

      【解决方案2】:

      就这样使用

      全局 $current_user;

      案例“计费电话”: 返回 $current_user->billing_phone; 休息;

      【讨论】:

      • 请添加一些解释,以便其他人能够理解!如果可能的话,添加您尝试过的代码。
      • 嗨,你不想使用 $phone = get_user_meta($current_user,'phone_number',true);返回$电话;而不是只使用我上面的就可以了。
      猜你喜欢
      • 2019-10-15
      • 1970-01-01
      • 2018-10-25
      • 2018-12-25
      • 2019-05-27
      • 2012-12-22
      • 2015-04-20
      • 1970-01-01
      • 2014-05-23
      相关资源
      最近更新 更多