Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 866
Raunak Gupta Avatar asked Nov 30 '16 04:11

Raunak Gupta


1 Answers

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:

  • get_checkout_payment_url()
  • wc_abstract_orderneeds_payment
  • woocommerce_valid_order_statuses_for_payment
like image 185
Raunak Gupta Avatar answered Nov 15 '22 16:11

Raunak Gupta