• Hello,
    I want to show html form data on wocommerce product page,
    I have created a template page and added my form on that page, and i figured out how to add display textbox or lable on product page but now i want users to add their custom data in the form and when they checkout for same product that data should be available for me in admin product order details.

    Please list me hooks/filter names that will help in achieving this goal
    so far i did this:

    I have used
    this to add text field and check box

    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
                function woo_add_custom_general_fields() {
    
                 global $woocommerce, $post;
                // Checkbox
                woocommerce_wp_checkbox(
                array(
                	'id'            => '_checkbox',
                	'wrapper_class' => 'show_if_simple',
                	'label'         => __('Check to allow', 'woocommerce' ),
                	'desc_tip' => 'true',
                	'description'   => __( 'check to show textbox data', 'woocommerce' )
                	)
                );
    
                	// Text Field
                woocommerce_wp_text_input(
                	array(
                		'id'          => '_text_field',
                		'label'       => __( 'My Text Field', 'woocommerce' ),
                		'placeholder' => 'https://',
                		'desc_tip'    => 'true',
                		'description' => __( 'Enter the custom value here.', 'woocommerce' )
                	)
                );
                }

    To save data

    // Save Fields
            add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
            function woo_add_custom_general_fields_save( $post_id ){
            	// Checkbox
            	$woocommerce_checkbox = isset( $_POST['_checkbox'] ) ? 'yes' : 'no';
            	update_post_meta( $post_id, '_checkbox', $woocommerce_checkbox );
            //textbox
            $woocommerce_text_field = $_POST['_text_field'];
            	if( !empty( $woocommerce_text_field ) )
            		update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
    
            }

    To display this text data on product page front end and link to custom form

    //display data
            add_action( 'woocommerce_single_product_summary', 'wc_rrp_show', 50 );
            function wc_rrp_show() {
                global $product;
            	$checkbox_result = get_post_meta( $product->id, '_checkbox', true );
    
            	if($checkbox_result=='yes') {
    
            		echo '<br>';
            		//link to my custom html form
            		echo ' <a style="color:#A46497;" href="<my form link>">Submit Data</a> ';
            		echo '<br>';
            		echo get_post_meta( get_the_ID(), '_text_field', true );
            	}
            }

    To display this text data in checkout page and admin order details page

    add_action( 'woocommerce_add_order_item_meta', 'majemedia_save_item_sku_order_itemmeta', 10, 3 );
        function majemedia_save_item_sku_order_itemmeta( $item_id, $values, $cart_item_key ) {
    
               $text_data  =  get_post_meta( $values[ 'product_id' ], '_text_field', true );
    
                wc_add_order_item_meta( $item_id, 'Your Measurments', $text_data , false );
        }
  • The topic ‘Wocommerce Add HTML Form Data On Product Page’ is closed to new replies.