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?

Thanks for help,

A