php - 在 Woocommerce 中禁用编辑特定的管理员自定义字段

标签 php wordpress woocommerce admin custom-fields

我的库存选项卡中有一些用于 Woocommerce 产品的自定义字段,使用 woocommerce_wp_text_input()功能。我想再添加一个自定义文本字段,仅用于显示引用值。

我想默认锁定文本字段,以免在其中写入内容。

是否可以?

最佳答案

是的,可以使用以下方法在字段参数数组中添加 as custom_attributes 只读属性:

'custom_attributes' => array('readonly' => 'readonly'),

所以你的代码会是这样的:
add_action( 'woocommerce_product_options_stock_status', 'display_product_options_inventory_custom_fields', 20 );
function display_product_options_inventory_custom_fields() {
    global $post;

    echo '</div><div class="options_group">'; // New separated section

    // Text field (conditionally readonly)
    woocommerce_wp_text_input( array(
        'id'                => '_text_field_ro',
        'type'        => 'text',
        'label'       => __( 'Read only field', 'woocommerce' ),
        'placeholder' => __( 'placeholder text', 'woocommerce' ),
        'description' => __( 'Custom description: your explanations.', 'woocommerce' ),
        'desc_tip'    => true,
        'custom_attributes' => $readonly, // Enabling read only
    ) );
}

代码位于事件子主题(或事件主题)的 function.php 文件中。测试和工作。

enter image description here

更新:添加复选框以启用只读字段:
add_action( 'woocommerce_product_options_stock_status', 'display_product_options_inventory_custom_fields', 20 );
function display_product_options_inventory_custom_fields() {
    global $post;

    echo '</div><div class="options_group">'; // New separated section

    // Checkbox
    woocommerce_wp_checkbox( array(
        'id'          => '_enable_readonly',
        'label'       => __( 'Enable readonly fields', 'woocommerce' ),
        'description' => __( 'Enable some fields to be readonly', 'woocommerce' ),
        'desc_tip'    => true,
    ));

    // Get the checkbox value
    $checkbox = get_post_meta( $post->ID, '_enable_readonly', true );

    // We set the field attribute "readonly" conditionally based on the checkbox
    $readonly = empty($checkbox) ? '' : array('readonly' => 'readonly');

    // Text field 1 (conditionally readonly)
    woocommerce_wp_text_input( array(
        'id'                => '_text_field_ro1',
        'type'        => 'text',
        'label'       => __( 'Read only field 1', 'woocommerce' ),
        'placeholder' => __( 'placeholder text 1', 'woocommerce' ),
        'description' => __( 'Custom description 1: your explanations.', 'woocommerce' ),
        'desc_tip'    => true,
        'custom_attributes' => $readonly, // Enabling read only
    ) );

    // Text field 2 (conditionally readonly)
    woocommerce_wp_text_input( array(
        'id'                => '_text_field_ro2',
        'type'        => 'text',
        'label'       => __( 'Read only field 2', 'woocommerce' ),
        'placeholder' => __( 'placeholder text 2', 'woocommerce' ),
        'description' => __( 'Custom description 2: your explanations.', 'woocommerce' ),
        'desc_tip'    => true,
        'custom_attributes' => $readonly, // Enabling read only
    ) );
}

add_action( 'woocommerce_process_product_meta', 'save_product_custom_fields' );
function save_product_custom_fields( $post_id ) {
    // 1. readonly checkbox
    $readonly = isset( $_POST['_enable_readonly'] ) ? esc_attr( $_POST['_enable_readonly'] ) : '';
    update_post_meta( $post_id, '_enable_readonly', $readonly );

    // 2. Readonly fields: allow saving when readonly is disabled
    if( ! isset( $_POST['_enable_readonly'] ) ){
        // Save text field 1 value
        if( isset( $_POST['_text_field_ro1'] ) ){
            update_post_meta( $post_id, '_text_field_ro1', sanitize_text_field( $_POST['_text_field_ro1'] ) );
        }
        // Save text field 2 value
        if( isset( $_POST['_text_field_ro2'] ) ){
            update_post_meta( $post_id, '_text_field_ro2', sanitize_text_field( $_POST['_text_field_ro2'] ) );
        }
    }
}

代码位于事件子主题(或事件主题)的 function.php 文件中。测试和工作。

复选框被禁用(字段不是只读的):

enter image description here

复选框已启用(字段为只读):

enter image description here

关于php - 在 Woocommerce 中禁用编辑特定的管理员自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52469006/

相关文章:

php - php中是否有一个内置功能可以将秒数转换为hh :mm:ss

wordpress - 如何通过比较 acf 自定义字段日期值来过滤过去一年的自定义帖子

php - JQuery AJAX 发布并刷新页面内容,无需重新加载页面

php - 在 Woocommerce 中为类别应用优惠券时自定义购物车商品价格

php - WooCommerce Checkout 必填字段与计费城市字段有关

php - 使用 VM 和 Apache 进行开发时收到 403 错误

PHP PDO 更新多条记录而不是一条记录

javascript - 将新数据添加到数据库后更新下拉列表

css - 高级自定义字段 CSS

php - WooCommerce:获取 SQL 查询中产品变体的总销售额