在 WooCommerce 上添加一个支付订单按钮我的帐户查看挂单订单

Add a pay order button on WooCommerce My account view order for pending orders

如何在订单状态为 pending 时创建“为此订单付款”按钮,以便在查看订单时显示在我的帐户页面上

link结构如下:

https://url.com/checkout/order-pay/XXXXX/?pay_for_order=true&key=wc_order_XXXXXXXXXXXX

add_action( 'woocommerce_view_order', 'order_pay_button' );
function order_pay_button( $order_id ){
// Get an instance of the `WC_Order` Object
$order = wc_get_order( $order_id );

// Get the order number
$order_number  = $order->get_order_number();

    
// Get the order status name
$status_name  = wc_get_order_status_name( $order->get_status() );

// Get the order key
 $test_order_key = $order->get_order_key();
 
   
// Display the order status 
echo '<div>' . __("Order Status:") . ' ' . $status_name . '</div>';



if ($status_name == "pending") {
   
     echo '
<a href="https://url.com/checkout/order-pay/'.''.$order_number.''.'/?pay_for_order=true&key='.''.$test_order_key.''.'">
<button type="submit" class="button alt" id="place_order">Pay for this order</button></a>';
   
} 


}

尝试以下简化代码版本,它将显示挂单的支付订单按钮:

add_action( 'woocommerce_view_order', 'order_pay_button' );
function order_pay_button( $order_id ){
    // Get an instance of the `WC_Order` Object
    $order = wc_get_order( $order_id );

    if ( $order->get_status() == "pending" ) {
        printf(
            '<a class="woocommerce-button button pay" href="%s/order-pay/%s/?pay_for_order=true&key=%s">%s</a>',
            wc_get_checkout_url(), $order_id, $order->get_order_key(), __("Pay for this order", "woocommerce")
        );
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。