skip to Main Content

The following answer code is not working, as I use the custom order status pre-ordered:

Change admin payment status back to unpaid for pending order status in Woocommerce

All my products are pre-orders and for that I use a pre-order plugin (from Bright) and the custom payment plugin (from WPRuby), which I have named "Betaal later" (which means "pay later"). Then I have created a custom order status "Pre Ordered", and I have set up this custom status in the custom payment plugin settings, so every order using this payment option, get’s the status Pre Ordered.

If the customer makes an order and the only payment option is ‘Betaal later’, then the order already gets a paid date (the date of the creation of the order) and that’s not correct because it is not paid. Can someone help me with this code to work for my situation? It looks like this answer code is not made to work with custom order statuses. Because I have tried to add pre-ordered in the code, but it gives a critical error.

Here is my code attempt:

add_action( 'woocommerce_order_status_changed', 'reset_order_paid_date', 20, 4 );
function reset_order_paid_date( $order_id, $old_status, $new_status, $order ){
    if ( in_array( $old_status, array('on-hold', 'processing', 'completed', 'pending') ) && $new_status == 'pre-ordered') {
        $order->set_date_paid(null);
        $order->save();
    }
}

Here are some screenshots:

enter image description here

enter image description here

2

Answers


  1. Try the following for your pre-ordered custom status:

    add_action( 'woocommerce_order_status_pre-ordered', 'reset_order_paid_date_for_pre_orders', 900, 2 );
    function reset_order_paid_date_for_pre_orders( $order_id, $order ){
        if ( $order->get_date_paid() ) {
            $order->set_date_paid(null);
            $order->save();
        }
    }
    

    It should work.


    Addition

    You can try to add the following too:

    add_action( 'woocommerce_order_status_changed', 'reset_order_paid_date', 900, 4 );
    function reset_order_paid_date( $order_id, $old_status, $new_status, $order ){
        if ( $new_status !== 'completed' && $order->get_date_paid() ) {
            $order->set_date_paid(null);
            $order->save();
        }
    }
    

    It could solve the issue.

    Login or Signup to reply.
  2. You can use the standard woocomerce update_status command. Example:

    add_action('woocommerce_order_status_pending', 'custom_change_payment_status_to_pending', 10, 2);
    
    function custom_change_payment_status_to_pending($order_id) {
        // Change payment status to Pending
        $order = new WC_Order( $order_id );
        $order->update_status( 'pending' );
    }
    

    You can add this code in your active theme’s functions.php file or in a custom plugin

    To correct the text describing the payment in the order notes in this role in WooCommerce, you can include this procedure. Example:

    // Check if the order exists and if it's a pending order
        if ($order && 'pending' === $order->get_status()) {
            // Loop through all the order notes
            foreach ($order->get_customer_order_notes() as $note) {
                // Check if the note contains the incorrect text you want to correct
                if (strpos($note->get_content(), 'Paid on September 17, 2018 at 9:18 AM') !== false) {
                    // Replace the incorrect text with the correct text
                    $new_content = str_replace('Paid on September 17, 2018 at 9:18 AM', 'Pending - Corrected Text', $note->get_content());
                    
                    // Update the note's content with the new text
                    $note->set_content($new_content);
                    
                    // Save the changes to the note
                    $note->save();
                }
            }
        }
    

    Be sure to test this in a staging environment before implementing it on your production site.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search