How to get Pay Now URL with custom order status in WooCommerce?
Asked Answered
E

2

6

I want to get the URL from where the customer can directly pay for their Invoice and also it should work with wc-cancelled and wc-transaction-declined (custom order status).

My Solution
What I'm doing now is created a custom page with my custom get parameters and processing the whole Payment Process as Documentation in Gateway provider Website.

My Problem
But the problem is whenever they update their doc file and plugin I also have to update my code; but if I get the Pay Now URL then WooCommerce and Gateway Plugin will take care of it.

Is there a better solution?

Enterpriser answered 30/11, 2016 at 4:46 Comment(0)
E
19

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

Usage

$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.

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

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


Reference:

Enterpriser answered 30/11, 2016 at 4:49 Comment(3)
This is very useful… nice shot :)Repeater
thanks @LoicTheAztec, I had struggle for this, so thought to contribute this in SO, so that someone else will get benefited.Enterpriser
Need a shortcode can provide the pay-order link, example: domaincom/checkout/order-pay/9374/… Have some idea how can achieve that?Lory
P
0

You can get url with below code but it will work for wc-pending status order only, with the help of order_id or post_id

$order = wc_get_order($order_id);
echo $pay_now_url = $order->get_checkout_payment_url();
Punctual answered 4/10, 2021 at 18:12 Comment(2)
Hi Vivekanand, yes that's true, please check the second part of this answer and it is always recommended to use the native method. Because if WooCommerce change there logic of URL then you have to do that same for your code. But if you are using the native method then WooCommerce will handle that.Enterpriser
Hi @RaunakGupta, I updated answer.Punctual

© 2022 - 2025 — McMap. All rights reserved.