Display Custom Fields
-
I currently have the custom fields displaying and saving on the admin side. Here’s that code.
// 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 Material Name woocommerce_wp_text_input( array( 'id' => '_material_field', 'placeholder' => 'Material', 'label' => __('Material', 'woocommerce'), 'desc_tip' => 'true' ) ); //Decoration Field woocommerce_wp_text_input( array( 'id' => '_decoration_field', 'placeholder' => 'Decoration', 'label' => __('Decoration', 'woocommerce'), 'desc_tip' => 'true' ) ); //Lamination Field woocommerce_wp_text_input( array( 'id' => '_lamination_field', 'label' => __('Lamination', 'woocommerce') ) ); //Handles Field woocommerce_wp_text_input( array( 'id' => '_handles_field', 'label' => __('Handles', 'woocommerce') ) ); //Closure Field woocommerce_wp_text_input( array( 'id' => '_closure_field', 'label' => __('Closure', 'woocommerce') ) ); //Lining Field woocommerce_wp_text_input( array( 'id' => '_lining_field', 'label' => __('Lining', 'woocommerce') ) ); //Minimum Field woocommerce_wp_text_input( array( 'id' => '_minimum_field', 'label' => __('Minimum', 'woocommerce') ) ); echo '</div>'; } function woocommerce_product_custom_fields_save($post_id) { // Material $woocommerce_material_field = $_POST['_material_field']; if (!empty($woocommerce_material_field)) update_post_meta($post_id, '_material_field', esc_attr($woocommerce_material_field)); // Decoration $woocommerce_decoration_field= $_POST['_decoration_field']; if (!empty($woocommerce_decoration_field)) update_post_meta($post_id, '_decoration_field', esc_attr($woocommerce_decoration_field)); // Laminiation $woocommerce_lamination_field = $_POST['_lamination_field']; if (!empty($woocommerce_lamination_field)) update_post_meta($post_id, '_lamination_field', esc_html($woocommerce_lamination_field)); // Handles $woocommerce_handles_field = $_POST['_handles_field']; if (!empty($woocommerce_handles_field)) update_post_meta($post_id, '_handles_field', esc_html($woocommerce_handles_field)); // Closure $woocommerce_closure_field = $_POST['_closure_field']; if (!empty($woocommerce_closure_field)) update_post_meta($post_id, '_closure_field', esc_html($woocommerce_closure_field)); // Lining $woocommerce_lining_field = $_POST['_lining_field']; if (!empty($woocommerce_lining_field)) update_post_meta($post_id, '_lining_field', esc_html($woocommerce_lining_field)); // Minimum $woocommerce_minimum_field = $_POST['_minimum_field']; if (!empty($woocommerce_minimum_field)) update_post_meta($post_id, '_minimum_field', esc_html($woocommerce_minimum_field)); } //Display custom fields on backend 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) ); } }
I have this code, but it isn’t displaying.
function aq_wc_insert_additional_information() { while (have_posts()) : the_post(); wc_get_template_part('content', 'single-product'); // Display the value of custom product text field echo get_post_meta($product->ID(), '_custom_product_text_field', true); // Display the value of custom product number field echo get_post_meta(get_ID(), '_custom_product_number_field', true); // Display the value of custom product text area echo get_post_meta(get_ID(), '_custom_product_textarea', true); endwhile; // end of the loop. } add_action( 'woocommerce_display_product_attributes', 'aq_wc_insert_additional_information' );
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Display Custom Fields’ is closed to new replies.