php - 隐藏自定义结帐字段,使其不会出现在 WooCommerce 我的帐户编辑地址中
问题描述
我已经使用 wooocmmerec 结帐字段编辑器插件创建了一个 woo-commerce 结帐字段,如下图所示,它应该有条件地出现在结帐时,这是启用它的代码。
function ga_checkout_fields($fields) {
global $user_country;
//$user_country = 'in'; //for test in dev
if( $user_country != 'in') { //hide gst if the location is not india
unset($fields['billing']['billing_gst_number']);
}
if( $user_country != 'br') { //hide taxid if the location is not brazil
unset($fields['billing']['billing_br_tax_id']);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ga_checkout_fields' );
此代码在结帐时完美运行,但问题是当用户尝试在我的帐户中编辑他们的帐单地址时,他们会看到这些自定义字段并且无法编辑他们的地址。我不想在我的帐户编辑帐单地址表单中向客户显示或保存自定义字段。我尝试使用以下代码在我的帐户中的帐单地址取消设置这些自定义字段,我可以隐藏这些字段但无法保存它们,因为结帐时需要税号字段。我希望在结帐时需要税号,并且我不想在我的帐户中的编辑帐单地址中有任何自定义字段?关于如何实现这一目标的任何见解?
function ga_remove_the_address_field( $address, $load_address ) {//this removes fields in the edit form
global $user_country;
if( $user_country != 'in') { //hide gst if the location is not india
unset($address['billing_gst_number']);
}
if( $user_country != 'br') { //hide taxid if the location is not brazil
unset($address['billing_br_tax_id']);
}
return $address;
}
add_filter( 'woocommerce_address_to_edit', 'ga_remove_the_address_field', 10, 2 );
解决方案
要从我的帐户中隐藏自定义字段 > 编辑帐单地址,您将使用以下内容:
add_filter( 'woocommerce_billing_fields', 'ga_remove_account_custom_fields', 1000 );
function ga_remove_account_custom_fields( $fields ) {
// Only on My account > Edit address section
if ( is_wc_endpoint_url('edit-address') ) {
// Here define the custom field Ids to hide in the array
$field_ids = array('billing_gst_number', 'billing_br_tax_id');
foreach ( $field_ids as $field_id ) {
if( isset($fields[$field_id]) ) {
unset($fields[$field_id]);
}
}
}
return $fields;
}
代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。
注意:对于运输字段,您将使用挂钩woocommerce_shipping_fields
。
有关的:
推荐阅读
- java - 如何在firebase中获取所有孩子的数据并将其显示到我的android应用程序中?
- datatable - 合并的数据表不要复制到新表单
- ios - 从一个位置移动到另一个位置后 UIButton 框架发生了变化
- javascript - Node.js Knex SQL 插入太慢
- javascript - 计算json Angular 5中的值数
- wordpress - Wordpress 插件文件不会在自定义主题上加载
- r - R中带有for循环的矩阵
- oracle - 使用 OCI 获取/设置 TIMESTAMP 值
- html - {unicode-bidi: bidi-override} + @Keyframes /*除了 Chrome 之外不起作用*/
- java - 如何在坐标系中显示大坐标?