通过设计,所有三个表单--Check Out、REGISTER和EDIT_ACCOUNT是相同的.所以我必须添加所有需要的字段来编辑帐户表单,因为从我的帐户编辑地址表单在设计上根本不需要.我已经添加了所有的字段,但与国家字段有一些问题.它必须与 Select 器2一起使用,一切都很好,但它不会更新. 创建新用户时,它会将国家/地区保存在用户元数据中. 但在编辑帐户页面中,我无法更改国家/地区.更新后,它仍然是注册时保存的那个.为了进行比较,我也复制了城市字段作为一个示例.

add_action( 'woocommerce_edit_account_form', 'add_billing_phone_to_edit_account_form' );
function add_billing_phone_to_edit_account_form() {
    $user = wp_get_current_user();
    ?>
<div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">  
            <label for="reg_billing_city"><?php _e( 'Town/City', 'woocommerce' ); ?></label>
            <input type="text" name="billing_city" id="reg_billing_city" placeholder="<?php _e( 'Town/City', 'woocommerce' ); ?>" value="<?php echo get_user_meta( get_current_user_id(), 'billing_city', true );?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" />
        </div>

<?php
    wp_enqueue_script('wc-country-select');
    woocommerce_form_field('billing_country',array(
        'type'        => 'country',
        'class'       => array('dop'),
        'label'       => __('Country'),
        'placeholder' => __('Choose your country.'),
        'required'    => true,
        'clear'       => true,
        'default'     => 'FR'
    ));
}
add_action( 'woocommerce_save_account_details', 'save_billing_city_account_details', 16, 1 );
function save_billing_city_account_details( $customer_id ) {    
     if( isset( $_POST['billing_city'] ) )
        update_user_meta(  $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );           
    }



add_action( 'woocommerce_save_account_details', 'save_billing_country_account_details', 17, 1 );
function save_billing_country_account_details( $customer_id  ) {    
     if( isset( $_POST['billing_country'] ) )
        update_user_meta(  $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );         
}

我try 使用$USER_ID而不是$CUSTOMER_ID--它没有帮助. 对于City,我使用相同的方法进行更新,而City更新得很好.如邮政编码、电话等 这个国家有什么特别之处?

推荐答案

"BILLING_COUNTRY"的woocommerce_form_field()函数中缺少参数.国家/地区已保存在数据库中,但在重新加载时不会显示在该字段中.

我重新查看了您的代码,合并了最后两个函数,并更改了国家/地区值清理.

add_action( 'woocommerce_edit_account_form', 'add_custom_billing_fields_to_edit_account_form' );
function add_custom_billing_fields_to_edit_account_form() {
    $user = wp_get_current_user();
    ?>
    <div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">  
        <label for="reg_billing_city"><?php _e( 'Town/City', 'woocommerce' ); ?></label>
        <input type="text" name="billing_city" id="reg_billing_city" placeholder="<?php _e( 'Town/City', 'woocommerce' ); ?>" value="<?php echo isset($user->billing_city) ? $user->billing_city : '';?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" />
    </div>
    <?php
    wp_enqueue_script('wc-country-select');

    woocommerce_form_field('billing_country', array(
        'type'        => 'country',
        'class'       => array('dop'),
        'label'       => __('Country'),
        'placeholder' => __('Choose your country.'),
        'required'    => true,
        'clear'       => true,
        'default'     => 'FR'
    ), isset($user->billing_country) ? $user->billing_country : 'FR' ); // <== HERE this was missing
}

add_action( 'woocommerce_save_account_details', 'save_account_details_custom_billing_fields', 20 );
function save_account_details_custom_billing_fields( $customer_id ) {    
     if( isset( $_POST['billing_city'] ) ) {
        update_user_meta(  $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) ); 
     } 
    
     if( isset( $_POST['billing_country'] ) ) {
         update_user_meta(  $customer_id, 'billing_country', esc_attr( $_POST['billing_country'] ) ); // Changed sanitization
     } 
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

Php相关问答推荐

将一个数字分配到一系列因素中(就像贪婪算法)

composer如何解析包名?

我的docker—compose lamp stack在软件更新后不工作:Containerconfig错误'''

在WooCommerce Checkout帐单地址字段后插入自定义按钮

以编程方式更新现有Yith WooCommerce Booking的状态

有什么方法可以阻止WordPress主题升级通知吗?

如何减少Laravel分页中的链接数

使用php创建表格和插入数据

Laravel:如何展平多维Eager 加载的集合

在WP工具中页面加载时可短暂看到的JavaScript生成的字段

在php中,方法之间的属性链接是如何工作的

对 WooCommerce 购物车和 checkout 中显示的运输方式进行排序

Woocommerce API - 图像问题

如果在 APIPlatform 3.1 中的同一资源上使用处理器,Messenger 将无法工作

使用自定义规则进行 Livewire 验证不会显示错误

跟踪通过 WooCommerce 分层定价表插件进行的购买

将Shopware 6后端中定义的值导入MultiFilter

WooCommerce checkout 流程中基于所选选项添加自定义费用

使用 RSA 2048 公钥验证 RSA PKCS#1v1.5 SHA 256 签名

Symfony 在将它传递给 Twig 时是否从数据库中获取整个对象?