Display a custom Woocommerce product input field in the YITH Request a Quote table

3 min read 05-10-2024
Display a custom Woocommerce product input field in the YITH Request a Quote table


Displaying a Custom Input Field in Your YITH Request a Quote Table: A Step-by-Step Guide

Problem: You're using WooCommerce and YITH Request a Quote to offer personalized pricing on specific products. However, you need to collect extra information from customers before generating a quote. This information might be specific product dimensions, custom engraving requests, or any other details relevant to your products.

Solution: By leveraging the power of custom fields and hooks in WooCommerce and YITH Request a Quote, you can add a custom input field to your quote request form and gather the necessary information directly from your customers.

Scenario:

Imagine you sell personalized mugs. You need to collect the name or text that the customer wants engraved on the mug before creating a quote. Let's see how to add a custom input field to capture this information.

Original Code:

Let's assume you're currently using the default YITH Request a Quote form without any custom fields.

Step 1: Adding the Custom Field

To add a custom field to your quote request form, you'll need to utilize the yith_wcqr_add_custom_fields_before_button action hook provided by YITH Request a Quote. Here's how:

add_action( 'yith_wcqr_add_custom_fields_before_button', 'add_custom_field_to_quote_form' );

function add_custom_field_to_quote_form() {
    ?>
    <div class="form-row">
        <label for="engraving_text">Engraving Text:</label>
        <input type="text" id="engraving_text" name="engraving_text" placeholder="Enter your text here">
    </div>
    <?php
}

Explanation:

  • add_action( 'yith_wcqr_add_custom_fields_before_button', 'add_custom_field_to_quote_form' );: This line hooks your custom function (add_custom_field_to_quote_form) to the yith_wcqr_add_custom_fields_before_button action. This ensures your custom field is displayed right before the "Request a Quote" button.

  • add_custom_field_to_quote_form() function: This function creates the custom field. Inside the function, we use HTML to define a label (<label>) and an input field (<input>).

  • name="engraving_text": This is the name attribute that will be used to identify this field in the quote request data.

Step 2: Accessing the Custom Field Data

Now you need to access the engraving_text value submitted through the form and process it. YITH Request a Quote provides hooks for this purpose.

add_action( 'yith_wcqr_before_save_quote', 'process_custom_field_data', 10, 2 );

function process_custom_field_data( $quote_id, $product_id ) {
    $engraving_text = isset( $_POST['engraving_text'] ) ? sanitize_text_field( $_POST['engraving_text'] ) : '';
    
    // Store the engraving_text in the quote metadata.
    update_post_meta( $quote_id, 'engraving_text', $engraving_text ); 
}

Explanation:

  • add_action( 'yith_wcqr_before_save_quote', 'process_custom_field_data', 10, 2 );: This line hooks your function (process_custom_field_data) to the yith_wcqr_before_save_quote action, which is executed just before the quote is saved in the database.

  • process_custom_field_data( $quote_id, $product_id ) function: This function retrieves the custom field data from the $_POST array, sanitizes it for security purposes, and then stores it as metadata associated with the newly created quote.

Step 3: Utilizing the Custom Field Data

The custom field data you saved can now be accessed and used in different areas of your website. For example, you can:

  • Display the engraving text on the quote confirmation email: Use get_post_meta to access the engraving_text and include it in the email template.
  • Use the engraving text during order processing: When the customer accepts the quote, access the engraving_text using get_post_meta and include it in the order details.
  • Create a custom quote summary table: You can use the custom field value to add a new row in the quote summary table.

Conclusion:

Adding a custom input field to the YITH Request a Quote form using this approach allows you to collect valuable customer information, enriching your quote process and ultimately improving your customer experience. Remember to adapt the code to fit your specific needs and implement further customization as required.

Additional Notes:

  • This is just one example. You can add different types of input fields (text areas, dropdown menus, etc.) according to your requirements.
  • Always sanitize user input data to prevent security vulnerabilities.
  • Ensure that the custom field names and associated logic match your desired functionality.

Resources:

By following these steps, you can successfully add custom input fields to your YITH Request a Quote form, enhancing your quote process and making it easier to gather the information you need for personalized pricing.