I'm building a WordPress website on which medical professionnal offer services (appointment). My goal is to show on professionnal pages a form with :
Thanks in advance
You can try this
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(
array(
'id' => '_custom_product_text_field',
'placeholder' => 'Custom Product Text Field',
'label' => __('Custom Product Text Field', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
// save meta
function save_woocommerce_product_custom_fields($post_id)
{
$product = wc_get_product($post_id);
$custom_fields_woocommerce_title = isset($_POST['_custom_product_text_field']) ? $_POST['_custom_product_text_field'] : '';
$product->update_meta_data('_custom_product_text_field', sanitize_text_field($custom_fields_woocommerce_title));
$product->save();
}
add_action('woocommerce_process_product_meta', 'save_woocommerce_product_custom_fields');
// frontend display
function woocommerce_custom_fields_display()
{
global $post;
$product = wc_get_product($post->ID);
$custom_fields_woocommerce_title = $product->get_meta('woocommerce_custom_fields');
if ($custom_fields_woocommerce_title) {
printf(
'<div><label>%s</label><input type="text" id="woocommerce_product_custom_fields_title" name="woocommerce_product_custom_fields_title" value=""></div>',
esc_html($custom_fields_woocommerce_title)
);
}
}
add_action('woocommerce_before_add_to_cart_button', 'woocommerce_custom_fields_display');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.