【问题标题】:How to disable field in woocommerce checkout page [duplicate]如何禁用woocommerce结帐页面中的字段[重复]
【发布时间】: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


【解决方案1】:

根据您的情况添加is_checkout(),然后将字段更改为 billing_address_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()) || is_checkout()){

        $readonly = ['readonly' => 'readonly'];

        $billing_fields['billing']['billing_address_1']['custom_attributes'] = $readonly;
        $billing_fields['billing']['billing_address_2']['custom_attributes'] = $readonly;
    }
    return $billing_fields;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    相关资源
    最近更新 更多