doufan1899 2018-09-23 18:20 采纳率: 100%
浏览 639
已采纳

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

I have some custom fields for my Woocommerce products inside my Inventory Tab using woocommerce_wp_text_input() function. I would like to add one more custom text field just for displaying a value for reference.

I want to lock the textfield by default so as not to be able to write something in it.

Is it possible?

  • 写回答

1条回答 默认 最新

  • donglei_0507 2018-09-24 20:13
    关注

    Yes it is possible adding the as custom_attributes the readonly property in the field arguments array using:

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

    So your code will be like:

    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
        ) );
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    enter image description here


    Update: Adding a checkbox to enable the readonly fields:

    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'] ) );
            }
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    Checkbox is disabled (fields are not readonly):

    enter image description here

    Checkbox is enabled (fields are readonly):

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名