在WooCommerce我的帐户上添加付款订单按钮查看待定订单的订单

4ioopgfo  于 2022-10-24  发布在  WordPress
关注(0)|答案(1)|浏览(184)

如何在订单状态为pending时创建“Pay for This Order”按钮,以便在查看订单时显示在“My Account”页面上
链接结构如下:
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>';

} 

}
shyt4zoc

shyt4zoc1#

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

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

代码放在活动子主题(或活动主题)的函数.php文件中。经过测试,效果良好。

相关问题