在 Woocommerce 中为简单产品添加自定义设置选项卡

Adding custom settings tab for simple products in Woocommerce

我在尝试将名为 Discount_info 的自定义字段添加到简单产品时遇到问题。

我创建了一个名为 discount_info 的新选项卡,它可以很好地显示在简单的产品视图中。问题是尝试将自定义数字字段添加到此选项卡。我正在使用下面导致 500 错误的代码。我哪里出错了?

// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_discount_info', 
'woocom_general_product_data_custom_field' );

function woocom_general_product_data_custom_field() {
// Create a custom text field


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

}

 // Hook to save the data value from the custom fields
 add_action( 'woocommerce_process_product_meta', 
 'woocom_save_general_proddata_custom_field' );

 /** Hook callback function to save custom fields information */
  function woocom_save_general_proddata_custom_field( $post_id ) {

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

 }

没有足够的信息来帮助了解确切的问题,但值得使用更简洁的代码版本进行测试:

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

}

首先删除所有相关代码,然后试试这个:

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

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。