繁体   English   中英

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

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

当我为客户创建自定义订单时,他们必须在自己的帐户中付款。 但是当他们访问订单支付页面时,没有显示发货和账单字段。 怎么做?

我知道模板是 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 ); ?>

然后,您可以通过AJAX调用(通过添加新按钮)来更新订单元值。 希望这可以帮助。

好吧,我知道我来不及发布问题的答案,但我认为这会帮助其他用户。

您可以在 function.php 文件中使用以下代码。 我使用了@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');

并使用 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.

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