php - 如何使结帐帐单字段在 Woo Commerce 中只读
问题描述
我们有一个订单网站,然后交付。现在的问题是,我们想利用 My Account Address Billing 中的详细信息作为发货地址,在结帐时,我们希望从那里填充该地址,但不允许用户在结帐页面 Billing 表单上进行任何更改。
这段代码:
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;
}
仅使地址部分不被更改,但是可以修改名字,姓氏,手机等所有其他内容。
我们需要锁定所有这些字段,就像代码将地址字段锁定为只读一样。
解决方案
您可以删除if($key == 'billing_address_1' || $key == 'billing_address_2'){
此行的所有字段。检查贝洛代码。
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 ){
$key_value = get_user_meta($user_id, $key, true);
if( $key_value != '' ){
if( $key == 'billing_country' || $key == 'billing_state' || $key == 'billing_suburb' ){
$checkout_fields['billing'][$key]['custom_attributes'] = array('disabled'=>'disabled');
}else{
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
对于billing_country
,billing_state
和billing_suburb
,您必须在 hidden 中传递值,因为当您单击下订单时,选择下拉菜单禁用了选项值,为了解决这个问题,我们可以使用我们的值添加隐藏字段。
add_action('woocommerce_after_order_notes', 'billing_countryand_state_hidden_field');
function billing_countryand_state_hidden_field($checkout){
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
echo '<input type="hidden" class="input-hidden" name="billing_country" value="'.get_user_meta($user_id, 'billing_country', true).'">';
echo '<input type="hidden" class="input-hidden" name="billing_state" value="'.get_user_meta($user_id, 'billing_state', true).'">';
echo '<input type="hidden" class="input-hidden" name="billing_suburb" value="'.get_user_meta($user_id, 'billing_suburb', true).'">';
}
测试和工作
推荐阅读
- c++ - 使用单独的线程执行 C++ 中的命令列表
- gdb - 缓冲区溢出堆栈金丝雀位置
- javascript - 需要帮助格式化 Phonegap/Cordova 应用程序的小时、分钟和秒。在 Android 上工作,但不是在 ios 上工作 .. 让 Nan
- python - 如何查看 Dash 应用中的活跃用户数?
- javascript - 没有重复和固定长度的数组的排列
- javascript - 有没有办法隐藏您最小化浏览器或从某个窗口失去焦点的事实
- mysql - 查找从上一个日历月开始增加流式传输时间的用户
- java - Intellij IDE 在实体中显示错误突出显示
- android - 如何获取 android OAuth 的令牌?
- eclipse - Eclipse 2020-03 上 SCALA-Ide 插件的性能调优