skip to Main Content

I need to add Input field in the woo-commerce product so that I can show it in front end

3

Answers


  1. you can add following code to you active function.php file

    it will give three custom field for your

    //this code for adding field in product backend
    // Display Fields
    add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields1');
    // Save Fields
    add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
    function woocommerce_product_custom_fields1()
    {
        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' => __('Zoom Meeting Url ', 'woocommerce'),
            'desc_tip' => 'true'
        ));
        //Custom Product Number Field
        woocommerce_wp_text_input(array(
            'id' => '_custom_product_number_field',
            'placeholder' => 'Custom Product Text Field',
            'label' => __('Meeting ID', 'woocommerce'),
            'type' => 'text',
            'custom_attributes' => array(
                // 'step' => 'any',
                // 'min' => '0'
            )
        ));
        //Custom Product  Textarea
        woocommerce_wp_textarea_input(array(
            'id' => '_custom_product_textarea',
            'placeholder' => 'Custom Product Text',
            'label' => __('Password', 'woocommerce')
        ));
        echo '</div>';
    }
    function woocommerce_product_custom_fields_save($post_id)
    {
        // Custom Product Text Field
        $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
        if (!empty($woocommerce_custom_product_text_field))
            update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
        // Custom Product Number Field
        $woocommerce_custom_product_number_field = $_POST['_custom_product_number_field'];
        if (!empty($woocommerce_custom_product_number_field))
            update_post_meta($post_id, '_custom_product_number_field', esc_attr($woocommerce_custom_product_number_field));
        // Custom Product Textarea Field
        $woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea'];
        if (!empty($woocommerce_custom_procut_textarea))
            update_post_meta($post_id, '_custom_product_textarea', esc_html($woocommerce_custom_procut_textarea));
    }
    
    Login or Signup to reply.
  2. and In order to show that value in the frontend, you can following code

    <?php
    $id = $product->id;
    echo get_post_meta($id, '_custom_product_text_field', true);
    echo get_post_meta($id, '_custom_product_number_field', true);
    echo get_post_meta($id, '_custom_product_textarea', true);
    ?>
    
    Login or Signup to reply.
  3. If You wish to send that value to the email in order email to the client after they purchase the product

    we can add the following code in function.php using WooCommerce hook

    add_action( 'woocommerce_email_after_order_table', 'wdm_add_shipping_method_to_order_email', 10, 2 );
    function wdm_add_shipping_method_to_order_email( $order, $is_admin_email ) {
        echo '<h4>Zoom Meeting Details</h4>';
        $order_id = $order->get_id();
        $order = wc_get_order( $order_id ); //returns WC_Order if valid order 
        $items = $order->get_items();   //returns an array of WC_Order_item or a child class (i.e. WC_Order_Item_Product)
        foreach( $items as $item ) {
        $type = $item->get_type();
        $product_id = $item->get_product_id();
        echo "<p>" . get_the_title($product_id) . "</p>";
        echo "<p><strong>Zoom Meeting Url:</strong>" . get_post_meta($product_id, '_custom_product_text_field', true) . "</p>";
        echo "<p><strong>Meeting ID:</strong>" . get_post_meta($product_id, '_custom_product_number_field', true) . "</p>";
        echo "<p><strong>Password:</strong>" . get_post_meta($product_id, '_custom_product_textarea', true) . "</p>";
    
    
        //more code
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search