0

I am using wordpress and woocommerce, and I have a code for auto complete processing order, but the php was executed twice at the same time, and it causes some problems, is there any method I can avoid this, like is there a function for php like setTimeout in js? will it works? https://prnt.sc/R-ygNBOtkVqh

The code:(a very simple code)

add_filter( 'woocommerce_payment_complete_order_status', 'bbloomer_autocomplete_processing_orders', 9999 );
function bbloomer_autocomplete_processing_orders() {
   return 'completed';
} 
  • What payment gateway are you using? What version of wordpress/woocommerce are you using? Are there other plugins you have that may be relevant to the issue? If so, what are their versions? This seems like something an upgrade may be able to fix. Take a look at this fix: https://github.com/woocommerce/woocommerce-gateway-stripe/pull/1083 – Ben Borchard Nov 02 '22 at 20:56
  • I am using the newest wordpress and woocommerce, and this error only happened sometimes, like in 200 orders, there is one order like this, do you have any idea? – Nicholas Lee Nov 02 '22 at 21:46

1 Answers1

0

You can create a filter that will only run once by checking the current status. If the status is not 'complete' then update the status.

add_filter( 'woocommerce_payment_complete_order_status', 'bbloomer_autocomplete_processing_orders', 9999, 1 );
 
function bbloomer_autocomplete_processing_orders($order_id) {
    if (!$order_id) {
        return;
    }

    // Get an instance of the WC_Product object
    $order = wc_get_order($order_id);

    if ($order->get_status() !== 'complete') { 
        return 'completed';
    }
}


Also see this answer for more options: https://stackoverflow.com/a/35689563/6063906

Nintenic
  • 16
  • 3