admin管理员组

文章数量:1314216

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" >
</div>
<?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)

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" >
</div>
<?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)

Share Improve this question asked Dec 21, 2018 at 17:24 Kshitiz Mishra Kshitiz Mishra 111 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1
  1. Adding the new custom field on the WooCommerce checkout page

You can add the new custom field in various places on the Checkout page. WooCommerce has defined many actions for the checkout page. Ideally, I would like my fields after the billing or shipping address fields.

We will add a custom field below the billing address fields. To add that field below billing address we will use action woocommerce_after_checkout_billing_form.

<?php
add_action( 'woocommerce_after_checkout_billing_form', 'display_extra_fields_after_billing_address' , 10, 1 );
function display_extra_fields_after_billing_address () {
    _e( "Delivery Date: ", "add_extra_fields");
    ?>
    <br>
    <input type="text" name="add_delivery_date" class="add_delivery_date" placeholder="Delivery Date">
  <?php 
}

本文标签: Displaying Custom Input Value to Customer Order Details (My Account) page in Woocommerce