I've a problem with adding a new field in WooCommerce, before short description.
I used script in functions.php
and my new custom field is displayed correctly but:
- Short description disappear when use script, a new field is display fine.
- I can edit the content of a field on a product page, but I can't delete it. It is always the last value.
How can I display this custom field without removing the product short description?
How can I reset this custom field content?
Here is my code:
// Add a custom Field
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'Termin dostawy', 'woocommerce' ),
'placeholder' => 'np: 7 dni',
'desc_tip' => 'true',
'description' => __( 'Wpisz przewidywany termin dostawy, np: 7 dni.', 'woocommerce' )
)
);
}
// Save the custom field
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_text_field'];
if( ! empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}
// Display the custom field
add_action('woocommerce_short_description', 'magik_custom_text', 10, 1);
function magik_custom_text()
{
global $product;
global $woocommerce, $post;
echo "Termin dostawy: ".get_post_meta( $post->ID, '_text_field', true );
}
Function responsible for display:
add_action('woocommerce_short_description', 'magik_custom_text', 10, 1);
function magik_custom_text()
{