php - 在 WooCommerce 我的帐户上添加付款订单按钮查看待处理订单的订单

标签 php wordpress woocommerce payment orders

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

链接结构如下:

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 文件中。经过测试并有效。

关于php - 在 WooCommerce 我的帐户上添加付款订单按钮查看待处理订单的订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66518859/

相关文章:

php - 如何检查 WooCommerce 产品是否属于特定类别并在特定 Hook 处回显图像?

php - 在预算虚拟主机上运行的好 php 框架?

php - 如何从 html 调用 Php 函数

php - 干净的 URL - 我该如何实现这一点?

JavaScript 缓动

javascript - 下拉菜单转换为单选按钮,不再触发特定功能

php - 如何删除包裹在单选按钮周围的 div 标签?

php - 在账单地址 WooCommerce 结帐字段下添加一个段落

wordpress - Paypal 自适应支付插件不适用于 WooCommerce

php - 从 WooCommerce 中的所有优惠券中排除产品