简体   繁体   中英

Display billing and shipping fields in "Pay to order" page Woocommerce

When I create a custom order for clients, they have to pay it in their account. But when they access to the order pay page, no shipping and billing fields is showing. How to do it?

I know that the template is form-pay.php

Thanks

Add this to your theme's 'woocommerce/checkout/form-pay.php'.

<!-- Display Information -->
<h2 class="woocommerce-column__title"><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
<address>
    <?php echo wp_kses_post( $order->get_formatted_billing_address( __( 'N/A', 'woocommerce' ) ) ); ?>
    <?php if ( $order->get_billing_phone() ) : ?>
        <p class="woocommerce-customer-details--phone"><?php echo esc_html( $order->get_billing_phone() ); ?></p>
    <?php endif; ?>
    <?php if ( $order->get_billing_email() ) : ?>
        <p class="woocommerce-customer-details--email"><?php echo esc_html( $order->get_billing_email() ); ?></p>
    <?php endif; ?>
</address>

<h2 class="woocommerce-column__title"><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>
<address>
    <?php echo wp_kses_post( $order->get_formatted_shipping_address( __( 'N/A', 'woocommerce' ) ) ); ?>
</address>

<!-- Form -->
<h3><?php _e( 'Billing details', 'woocommerce' ); ?></h3>
<?php do_action( 'woocommerce_before_checkout_billing_form', $order ); ?>
<div class="woocommerce-billing-fields__field-wrapper">
    <?php
    $fields = WC()->checkout->get_checkout_fields( 'billing' );
    foreach ( $fields as $key => $field ) {
        $field_name = $key;

        if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
            $field['value'] = $order->{"get_$field_name"}( 'edit' );
        } else {
            $field['value'] = $order->get_meta( '_' . $field_name );
        }   
        woocommerce_form_field( $key, $field, $field['value'] );
    }
    ?>
</div>
<?php do_action( 'woocommerce_after_checkout_billing_form', $order ); ?>

<h3><?php _e( 'Shipping details', 'woocommerce' ); ?></h3>
<?php do_action( 'woocommerce_before_checkout_shipping_form', $order ); ?>
<div class="woocommerce-shipping-fields__field-wrapper">
    <?php
    $fields = WC()->checkout->get_checkout_fields( 'shipping' );
    foreach ( $fields as $key => $field ) {
        $field_name = $key;

        if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
            $field['value'] = $order->{"get_$field_name"}( 'edit' );
        } else {
            $field['value'] = $order->get_meta( '_' . $field_name );
        }   
        woocommerce_form_field( $key, $field, $field['value'] );
    }
    ?>
</div>
<?php do_action( 'woocommerce_after_checkout_shipping_form', $order ); ?>

You can then update the order meta values via AJAX call (by adding a new button). Hope this helps.

Well I know that I am too late to post answer to the question, but I think this will help other users.

You can use below code in to your function.php file. I have used some code from @outsource-wordpress answer

function add_shipping_billing(){
        $order_id = absint( get_query_var( 'order-pay' ) );
        $order = wc_get_order( $order_id );
    
        ?>
        <!-- Display Information -->
        <h2 class="woocommerce-column__title"><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
            <address>
            <?php echo wp_kses_post( $order->get_formatted_billing_address( __( 'N/A', 'woocommerce' ) ) ); ?>
            <?php if ( $order->get_billing_phone() ) : ?>
                <p class="woocommerce-customer-details--phone"><?php echo esc_html( $order->get_billing_phone() ); ?></p>
            <?php endif; ?>
            <?php if ( $order->get_billing_email() ) : ?>
                <p class="woocommerce-customer-details--email"><?php echo esc_html( $order->get_billing_email() ); ?></p>
            <?php endif; ?>
        </address>

        <h2 class="woocommerce-column__title"><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>
        <address>
            <?php echo wp_kses_post( $order->get_formatted_shipping_address( __( 'N/A', 'woocommerce' ) ) ); ?>
        </address>

        <!-- Form -->
        <h3><?php _e( 'Billing details', 'woocommerce' ); ?></h3>
        <?php do_action( 'woocommerce_before_checkout_billing_form', $order ); ?>
        <div class="woocommerce-billing-fields__field-wrapper">
            <?php
            $fields = WC()->checkout->get_checkout_fields( 'billing' );
            foreach ( $fields as $key => $field ) {
                $field_name = $key;

                if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
                    $field['value'] = $order->{"get_$field_name"}( 'edit' );
                } else {
                    $field['value'] = $order->get_meta( '_' . $field_name );
                }   
                woocommerce_form_field( $key, $field, $field['value'] );
            }
            ?>
        </div>
        <?php do_action( 'woocommerce_after_checkout_billing_form', $order ); ?>

        <h3><?php _e( 'Shipping details', 'woocommerce' ); ?></h3>
        <?php do_action( 'woocommerce_before_checkout_shipping_form', $order ); ?>
        <div class="woocommerce-shipping-fields__field-wrapper">
            <?php
            $fields = WC()->checkout->get_checkout_fields( 'shipping' );
            foreach ( $fields as $key => $field ) {
                $field_name = $key;

                if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
                    $field['value'] = $order->{"get_$field_name"}( 'edit' );
                } else {
                    $field['value'] = $order->get_meta( '_' . $field_name );
                }   
                woocommerce_form_field( $key, $field, $field['value'] );
            }
            ?>
        </div>
        <?php do_action( 'woocommerce_after_checkout_shipping_form', $order ); ?>
        
        <?php
         
    }

    add_action('woocommerce_pay_order_before_submit', 'add_shipping_billing');

And to update order's shipping and billing field use below code in function.php file.

    add_action('woocommerce_before_pay_action', 'do_woocommerce_before_pay_action');

    function do_woocommerce_before_pay_action($order){
        
        $order->set_shipping_first_name($_REQUEST['shipping_first_name'] ? $_REQUEST['shipping_first_name'] : null);
        $order->set_shipping_last_name($_REQUEST['shipping_last_name'] ? $_REQUEST['shipping_last_name'] : null);
        $order->set_shipping_company($_REQUEST['shipping_company'] ? $_REQUEST['shipping_company'] : null);
        $order->set_shipping_country($_REQUEST['shipping_country'] ? $_REQUEST['shipping_country'] : null);
        $order->set_shipping_address_1($_REQUEST['shipping_address_1'] ? $_REQUEST['shipping_address_1'] : null);
        $order->set_shipping_address_2($_REQUEST['shipping_address_2'] ? $_REQUEST['shipping_address_2'] : null);
        $order->set_shipping_city($_REQUEST['shipping_city'] ? $_REQUEST['shipping_city'] : null);
        $order->set_shipping_state($_REQUEST['shipping_state'] ? $_REQUEST['shipping_state'] : null);
        $order->set_shipping_postcode($_REQUEST['shipping_postcode'] ? $_REQUEST['shipping_postcode'] : null);
        
        
        
        $order->set_billing_first_name($_REQUEST['billing_first_name'] ? $_REQUEST['billing_first_name'] : null);
        $order->set_billing_last_name($_REQUEST['billing_last_name'] ? $_REQUEST['billing_last_name'] : null);
        $order->set_billing_company($_REQUEST['billing_company'] ? $_REQUEST['billing_company'] : null);
        $order->set_billing_country($_REQUEST['billing_country'] ? $_REQUEST['billing_country'] : null);
        $order->set_billing_address_1($_REQUEST['billing_address_1'] ? $_REQUEST['billing_address_1'] : null);
        $order->set_billing_address_2($_REQUEST['billing_address_2'] ? $_REQUEST['billing_address_2'] : null);
        $order->set_billing_city($_REQUEST['billing_city'] ? $_REQUEST['billing_city'] : null);
        $order->set_billing_state($_REQUEST['billing_state'] ? $_REQUEST['billing_state'] : null);
        $order->set_billing_postcode($_REQUEST['billing_postcode'] ? $_REQUEST['billing_postcode'] : null); 
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM