Reputation: 323
I am having a problem trying to add a custom field called Discount_info to a simple product.
I have created a new tab called discount_info which shows up in the simple product view just fine. Problem is trying to add a custom number field to this tab. I'm using the code below which is causing a 500 error. Any ideas where i am going wrong?
// 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 ) );
}
}
Upvotes: 2
Views: 1181
Reputation: 254373
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.
Upvotes: 1
Reputation: 1661
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 );
}
}
Upvotes: 0