【问题标题】:Add a pay order button on WooCommerce My account view order for pending orders在 WooCommerce 上添加支付订单按钮我的帐户查看订单待处理订单
【发布时间】:2021-06-05 16:29:49
【问题描述】:

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

链接结构如下:

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>';
   
} 


}

【问题讨论】:

    标签: php wordpress woocommerce payment orders


    【解决方案1】:

    试试下面的简化代码版本,它会为挂单显示一个支付订单按钮:

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 如何转换为简码以便将按钮放在任何地方?
    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 2021-02-12
    • 2021-04-19
    • 2020-11-03
    相关资源
    最近更新 更多