Search code examples
phpwordpresswoocommerceglobal-variablesorders

Prevent order go to processing status if condition is true


I tried many scenarios and hooks and at the end even in child template file for thankyou page

So if order is not failed and global variable == true switch to pending:

<?php if ($order->has_status('failed')) : ?>

//code

<?php else : ?>

<?php if ($GLOBALS['variable'] == true) : ?>


    <?php $order->update_status('pending'); ?>


<?php endif; ?>

Status is changing but the problem is that it is going from processing to pending. How to prevent going to processing status if condition ($GLOBALS['variable']) is true?


Solution

  • You can try the following for Cash on delivery (COD) payment method, replacing in the code both 'my_variable' with the correct slug:

    add_filter( 'woocommerce_cod_process_payment_order_status', 'conditionally_change_cod_order_status_to_pending', 10, 2 );
    function conditionally_change_cod_order_status_to_pending( $status, $order ) {
        $global_var = $GLOBALS; 
    
        if ( isset($global_var['my_variable']) && $global_var['my_variable'] ) {
            return 'pending';
        }
        return $status;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.