简体   繁体   中英

How to add custom fields addresses to WooCommerce product?

I'm building a WordPress website on which medical professionnal offer services (appointment). My goal is to show on professionnal pages a form with :

  • a select field with all the services (WooCommerce product) proposed by the professionnal
  • an other select field with all the addresses linked to the service choosed
  • an calendar with the available days of the service In order to allow a customer to book an service (appointment). In that way, I need to add addresses fields (that we can unlimited add) on WooCommerce product administration page. And I would like that these fields can be Autocompleted with Google Places API. How to add theses fields on a WooCommerce Product page ? I don't find any solution with ACF.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM