Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
1.9 kB
2
Indexable
add_filter( 'woocommerce_checkout_fields', 'disable_checkout_fields', 10, 1 );
add_filter( 'woocommerce_billing_fields', 'disable_account_billing_fields', 10, 1 );
add_filter( 'woocommerce_shipping_fields', 'disable_account_shipping_fields', 10, 1 );
add_filter( 'woocommerce_edit_account_form', 'disable_edit_account_fields', 10, 1 );

function disable_checkout_fields( $fields ) {
    foreach ( $fields['billing'] as $key => $field ) {
        if ( isset( $field['value'] ) && !empty( $field['value'] ) ) {
            $fields['billing'][$key]['custom_attributes'] = array('readonly' => 'readonly');
        }
    }
    foreach ( $fields['shipping'] as $key => $field ) {
        if ( isset( $field['value'] ) && !empty( $field['value'] ) ) {
            $fields['shipping'][$key]['custom_attributes'] = array('readonly' => 'readonly');
        }
    }
    return $fields;
}

function disable_account_billing_fields( $fields ) {
    foreach ( $fields as $key => $field ) {
        $fields[$key]['custom_attributes'] = array('readonly' => 'readonly');
    }
    return $fields;
}

function disable_account_shipping_fields( $fields ) {
    foreach ( $fields as $key => $field ) {
        $fields[$key]['custom_attributes'] = array('readonly' => 'readonly');
    }
    return $fields;
}

function disable_edit_account_fields() {
    ?>
    <style>
        #account_first_name, #account_last_name, #account_display_name, #account_email {
            pointer-events: none;
            background: #f1f1f1;
        }
    </style>
    <?php
}

add_filter( 'woocommerce_save_account_details_required_fields', 'allow_only_password_change', 10, 1 );

function allow_only_password_change( $required_fields ) {
    unset( $required_fields['account_first_name'] );
    unset( $required_fields['account_last_name'] );
    unset( $required_fields['account_display_name'] );
    unset( $required_fields['account_email'] );

    return $required_fields;
}
Leave a Comment