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

时间:2018-04-12 08:15:18

标签: php woocommerce tabs backend product

我在尝试将名为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 ) );
  }

 }

2 个答案:

答案 0 :(得分:1)

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

// 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文件中。经过测试和工作。

enter image description here

enter image description here

答案 1 :(得分:0)

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

docker run -p 127.0.0.1:5000:22 docker_image