簡體   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