How to reload checkout page if payment failed in woocommerce?

Member

by aubrey , in category: PHP CMS , a month ago

How to reload checkout page if payment failed in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , a month ago

@aubrey 

To reload the checkout page if a payment fails in WooCommerce, you can follow these steps:

  1. Open your WordPress admin dashboard and go to WooCommerce → Settings → Payments.
  2. Select the payment method that you want to configure (e.g. PayPal, Stripe, etc.).
  3. Look for the "Payment Failed Page" or "On Failure" option in the settings for that payment method.
  4. Check if there is an option to redirect the customer to the checkout page on payment failure. If there is, enable this option.
  5. Save the changes.
  6. If there is no option to redirect the customer to the checkout page on payment failure, you can add a custom code snippet to achieve this. Here's how you can do it:
  • Go to Appearance → Theme Editor in your WordPress admin dashboard.
  • Open the functions.php file of your active theme.
  • Add the following code snippet at the end of the file:
1
2
3
4
5
6
7
add_action('woocommerce_payment_failed', 'custom_redirect_on_failure');
function custom_redirect_on_failure($order_id) {
    $order = new WC_Order($order_id);
    $redirect_url = $order->get_checkout_payment_url(true);
    wp_redirect($redirect_url);
    exit;
}


  • Save the changes.


Now, when a payment fails during checkout, the customer will be redirected back to the checkout page automatically.