Woocommerce只读计费字段

Jor*_*rge 1 forms wordpress woocommerce

我有一些电子商务网站,在后端预定义了客户帐单地址。

我需要将“帐单邮寄地址”字段设置为“只读”,以避免客户替换放置在此处的信息...但是我不知道如何/在何处这样做...

可能吗?

zip*_*dan 5

将以下代码放入主题的“ function.php”文件中。

add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
    $current_user = wp_get_current_user();;
    $user_id = $current_user->ID;
    foreach ( $checkout_fields['billing'] as $key => $field ){
        if($key == 'billing_address_1' || $key == 'billing_address_2'){
            $key_value = get_user_meta($user_id, $key, true);
            if( strlen($key_value)>0){
                $checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
            }
        }
    }
    return $checkout_fields;
}
Run Code Online (Sandbox Code Playgroud)

此函数检查地址字段是否具有值(即,是否指定了地址),以及是否具有值,使该字段为只读。其他保持打开状态以为用户添加数据。希望这可以帮助。