Displaying Custom Input Value to Customer Order Details (My Account) page in Woocommerce


I have been looking for a solution to display a custom input field on Checkout page in Payment option area when Cash ON Delivery option is selected. Upon selection it displays a field in which customer inputs a value and the data is sent to the admin order details page. This is what I came up with.
I inserted this code in checkout/payment-method.php

<?php if ( $gateway->has_fields() || /* $gateway->get_description() && */ $gateway->id != "cod" ) : ?>
<?php endif; ?>
<?php if ( $gateway->id == "cod" ) : ?>
<div class="payment_box payment_method_<?php echo $gateway->id; ?>" <?php if ( ! $gateway->chosen ) : ?>style="display:none;"<?php endif; ?>>
<input type="text" name='cod_custom_field' placeholder="Enter Your Margin" >
<?php endif; ?>     

This is what it look like on checkout page Checkout COD custom box

Full template here

Here it is the code which resides in function.php

 * Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['cod_custom_field'] ) ) {
        update_post_meta( $order_id, 'COD Custom Field', sanitize_text_field( $_POST['cod_custom_field'] ) );

/** Field Entry Karne ke liye
 * Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><h2><strong><h2>'.__('Reseller Margin').':</h2></strong> ' . get_post_meta( $order->id,'COD Custom Field', true ) . '</h2></p>';

Here is the image what it looks like on checkout page
When I input a value it displays something like this in admin order details:
Admin Order Details

Now I want same value to be displayed on order details page on frontend as well. PLz tell me how do I do it? Here it is where I want that input value to be displayed with Label – “Margin or Your Bonus” Order details page on My account (customers)

Kshitiz Mishra 3 years 2020-03-31T04:51:26-05:00 0 Answers 83 views 0

Leave an answer