简体   繁体   English

在“按订单付款”页面中显示账单和运输字段 Woocommerce

[英]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我知道模板是 form-pay.php

Thanks谢谢

Add this to your theme's 'woocommerce/checkout/form-pay.php'. 将此添加到主题的“ 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). 然后,您可以通过AJAX调用(通过添加新按钮)来更新订单元值。 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.您可以在 function.php 文件中使用以下代码。 I have used some code from @outsource-wordpress answer我使用了@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.并使用 function.php 文件中的以下代码更新订单的运输和计费字段。

    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); 
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce 中删除管理员添加订单的国家/地区计费和运输字段 - Remove country billing and shipping fields on admin add order in Woocommerce 在 Woocommerce 的订单支付页面上询问并保存账单信息 - Ask for and save billing information on order-pay page in Woocommerce 在订单电子邮件 woocommerce 的帐单地址和送货地址中添加新添加的字段 - Adding newly added fields in billing address and shipping address in order emails woocommerce 在 WooCommerce 订单编辑页面中显示产品运输类别 - Display Products Shipping Classes in WooCommerce order edit page 如何在订单收到的woocommerce结账页面上更改送货地址和账单地址的文本? - How to change text of shipping address and billing address on order recieved page of woocommerce checkout? WooCommerce 管理员创建订单并在订单付款时添加运费 - WooCommerce Admin Create Order & Adding Shipping on Order Pay 在 WordPress 用户配置文件中隐藏 WooCommerce 计费和运输字段 - Hide WooCommerce billing and shipping fields in WordPress user profile 从Woocommerce中的账单/运输领域中删除手机? - Remove phone from billing/shipping fields everywhere in Woocommerce? 从 woocommerce 结帐账单运输字段中删除默认选择的 state - Remove default selected state from woocommerce checkout billing shipping fields WooCommerce 将订单送货地址作为单独的字段 - WooCommerce get order shipping address as separate fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM