2

I am working on a plugin that handles logic for when a user tries to checkout on the frontend and the order fails (e.g., person is using a credit card but it does not have enough balance to meet the cart totals, etc). I can see the order is in fact set as "FAILED" in WooCommerce, because I get the automated email from WC alerting admin of a failed order. I'm spending a lot of time testing out various actions and filters, and hoping someone could point me in the right direction.

0

You can catch specific status change by using this action hook 'woocommerce_order_status_{status}'. It is defined in the WC_Order class.

This is how you define it:

/**
 * Executed when the status is changed to failed.
 * @param int $order_id
 * @param \WC_Order $order
 */
function wpdg_9291_woocommerce_order_status_failed( $order_id, $order ) {
    // Do something here
}
add_action('woocommerce_order_status_failed', 'wpdg_9291_woocommerce_order_status_failed', 15, 2);
  • seems that hook only allows for one parameter passed in - $order_id – Zach Smith Jun 2 at 3:26
  • It does accept the second parameter and it's of the type WC_Order. If you look in the class the second parameter is passed as $this - that would be the instance of the current class WC_Order where the actual action is made. – gdarko Jun 2 at 8:08
1

When customer returns from a gateway without paying, it falls on order received (thankyou) page with 2 possible status: failed or pending, and there is 2 possible action hooks:

  • woocommerce_thankyou_{$order_payment_method} (composite hook with $order_id available argument)
  • woocommerce_thankyou (with $order_id available argument)

You can target that case with the following example:

add_action( 'woocommerce_thankyou', 'thankyou_action_callback', 10, 1 );
function thankyou_action_callback( $order_id ) {
    // Get an instance of the WC_Order Object
    $order = wc_get_order( $order_id );

    if( in_array( $order->get_status(), ['failed','pending'] ) ) {
        // Your code comes here
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.