【发布时间】:2021-01-02 06:54:39
【问题描述】:
我正在尝试禁用结帐页面上的地址 1 和地址 2 字段。 我在我的帐户页面上使用了以下工作代码,我尝试在结帐页面上编辑后使用代码来禁用地址 1 和地址 2 字段。但不幸的是它不起作用,请帮助。
add_filter( 'woocommerce_checkout_fields', 'readonly_billing_account_fields', 25, 1 );
function readonly_billing_account_fields ( $billing_fields ) {
// Only my account billing address for logged in users
if( is_user_logged_in() && is_account_page() ){
$readonly = ['readonly' => 'readonly'];
$billing_fields['address_1']['custom_attributes'] = $readonly;
$billing_fields['address_2']['custom_attributes'] = $readonly;
}
return $billing_fields;
}
【问题讨论】:
-
只需完全删除您的
IF语句,因此if( is_user_logged_in() && is_account_page() ){和相关的右括号作为挂钩woocommerce_checkout_fields仅用于结帐页面,因此不需要任何东西。 -
感谢您的评论,我试过了,但它对我没有用,无论如何我得到了答案:)
-
很抱歉,但这有效……不需要 if 语句,因为钩子
woocommerce_checkout_fields仅在结帐页面中触发。所以下面的答案是不正确的。现在$billing_fields['address_1']['custom_attributes']需要替换为$billing_fields['billing']['billing_address_1']['custom_attributes'] -
现在您的代码中的
$billing_fields['address_1']['custom_attributes'] = $readonly;需要替换为$billing_fields['billing']['billing_address_1']['custom_attributes'] = $readonly;和$billing_fields['address_2']['custom_attributes'] = $readonly;需要替换为$billing_fields['billing']['billing_address_2']['custom_attributes'] = $readonly;......它可以在没有任何IF 语句的情况下工作。
标签: php woocommerce filter-woocommerce