Home > Blockchain >  php is executed twice at the same time
php is executed twice at the same time

Time:11-05

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';
} 

CodePudding user response:

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

  • Related