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

こ雲淡風輕ζ 提交于 2019-12-05 06:47:03

问题


I want to get the URL from where 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 there 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?


回答1:


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


来源:https://stackoverflow.com/questions/40880509/how-to-get-pay-now-url-with-custom-order-status-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!