Adding custom settings tab for simple products in Woocommerce

為{幸葍}努か 提交于 2019-12-02 04:02:13

First remove all related code, and try this instead:

// Add a custom product setting tab to edit product pages options FOR SIMPLE PRODUCTS only
add_filter( 'woocommerce_product_data_tabs', 'discount_new_product_data_tab', 50, 1 );
function discount_new_product_data_tab( $tabs ) {
    $tabs['discount'] = array(
        'label' => __( 'Discount', 'woocommerce' ),
        'target' => 'discount_product_data', // <== to be used in the <div> class of the content
        'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
    );

    return $tabs;
}

// Add/display custom Fields in the custom product settings tab
add_action( 'woocommerce_product_data_panels', 'add_custom_fields_product_options_discount', 10 );
function add_custom_fields_product_options_discount() {
    global $post;

    echo '<div id="discount_product_data" class="panel woocommerce_options_panel">'; // <== Here we use the target attribute

    woocommerce_wp_text_input(  array(
        'type'          => 'number', // Add an input number Field
        'id'            => '_discount_info',
        'label'         => __( 'Percentage Discount', 'woocommerce' ),
        'placeholder'   => __( 'Enter the % discount.', 'woocommerce' ),
        'description'   => __( 'Explanations about the field info discount.', 'woocommerce' ),
        'desc_tip'      => 'true',
        'custom_attributes' => array(
            'step' => 'any',
            'min' => '1'
        ),
    ) );

    echo '</div>';
}

// Save the data value from the custom fields for simple products
add_action( 'woocommerce_process_product_meta_simple', 'save_custom_fields_product_options_discount', 50, 1 );
function save_custom_fields_product_options_discount( $post_id ) {
    // Save Number Field value
    $number_field = $_POST['_discount_info'];

    if( ! empty( $number_field ) ) {
        update_post_meta( $post_id, '_discount_info', esc_attr( $number_field ) );
    }
}

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


There aren't enough information to help know the exact problem, but it worth testing with a cleaner version of your code:

// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_discount_info', 'wc_general_product_data_custom_field' );
function wc_general_product_data_custom_field() {

        // Number Field
        woocommerce_wp_text_input( array(
            'id'                => '_discount_info',
            'label'             => __( 'Discount %', 'woocommerce' ),
            'desc_tip'          => 'true',
            'description'       => __( 'Enter the % discount here.', 'woocommerce' ),
            'type'              => 'number',
            'custom_attributes' => array(
                'min'   => '1',
                'step'  => '1',
            ),
        ) );

}

// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'wc_save_general_proddata_custom_field' );
/** Hook callback function to save custom fields information */
function wc_save_general_proddata_custom_field( $post_id ) {

    // Save Number Field
    $number_field = isset( $_POST['_discount_info'] ) ? $_POST['_discount_info'] : '';
    if( ! empty( $number_field ) ) {
        update_post_meta( $post_id, '_discount_info', $number_field );
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!