用 woocommerce_thankyou hook 添加地址

问题描述 投票:0回答:1

我想使用代码片段将订单账单和送货地址添加到 woocommerce 感谢页面的底部。

我已经修改了一个 PHP 片段,它确实可以在另一个页面上添加地址,但是在这里使用时会出现致命错误。

也许有人可以告诉我哪里出错了:

function thank_you_add_address(){
        $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>
        <?php
          }

    add_action('woocommerce_thankyou', 'thank_you_add_address');
php wordpress woocommerce hook-woocommerce checkout
1个回答
0
投票
function thank_you_add_address($order_id){
        //create an order instance 
        $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>
        <?php
          }

    add_action('woocommerce_thankyou', 'thank_you_add_address', 10, 1);

钩子

woocommerce_thankyou
接收
$order_id
作为参数。您可以使用它来创建订单对象

© www.soinside.com 2019 - 2024. All rights reserved.