且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在WooCommerce中获取具有自定义订单状态的“立即付款" URL?

更新时间:2022-12-10 20:01:51

我在WooCommerce templates/emails/customer-invoice.php文件中找到了解决方案.我一直在寻找的功能是get_checkout_payment_url().

I got the solution in WooCommerce templates/emails/customer-invoice.php file. The function that I was looking for is get_checkout_payment_url().

用法

$order = wc_get_order($order_id);
$pay_now_url = esc_url( $order->get_checkout_payment_url() );
echo $pay_now_url; //http://example.com/checkout/order-pay/{order_id}?pay_for_order=true&key={order_key}
//http://example.com will be site_url and protocol will depending upon SSL checkout WooCommerce setting.

但是此网址仅适用于pendingfailed订单状态;所以我用过滤器woocommerce_valid_order_statuses_for_payment

But this url only works with pending, failed order status; So I used filter woocommerce_valid_order_statuses_for_payment

if (!function_exists('filter_woocommerce_valid_order_statuses_for_payment')) {
    //http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderneeds_payment/
    //http://hookr.io/filters/woocommerce_valid_order_statuses_for_payment/
    // define the woocommerce_valid_order_statuses_for_payment callback 
    function filter_woocommerce_valid_order_statuses_for_payment( $array, $instance ) {
        $my_order_status = array('cancelled', 'transaction-declined');
        return array_merge($array, $my_order_status);
    }
    // add the filter 
    add_filter('woocommerce_valid_order_statuses_for_payment', 'filter_woocommerce_valid_order_statuses_for_payment', 10, 2);
}

^^我将此添加到了活动主题的functions.php文件中.

^^ I added this in my active theme's functions.php file.

参考:

  • get_checkout_payment_url()
  • wc_abstract_orderneeds_payment
  • woocommerce_valid_order_statuses_for_payment