WooCommerce , Auto cancel orders status from processing after X days

admin2025-01-07  9

Trying to use this code in function.php, but not working.. Any idea whats wrong?

// To change the amount of days just change '-7 days' to your liking
function get_unpaid_submitted() {        
            global $wpdb;

            $unpaid_submitted = $wpdb->get_col( $wpdb->prepare( "
                    SELECT posts.ID
                    FROM {$wpdb->posts} AS posts
                    WHERE posts.post_status = 'wc-processing'
                    AND posts.post_date < %s
            ", date( 'Y-m-d H:i:s', strtotime('-14 days') ) ) );

            return $unpaid_submitted;
    }

    // This excludes check payment type.
    function wc_cancel_unpaid_submitted() {        
            $unpaid_submit = get_unpaid_submitted();

            if ( $unpaid_submit ) {                
                    foreach ( $unpaid_submit as $unpaid_order ) {                        
                            $order = wc_get_order( $unpaid_order );
                            $cancel_order = True;

                            foreach  ( $order->get_items() as $item_key => $item_values) {                                
                                    $manage_stock = get_post_meta( $item_values['variation_id'], '_manage_stock', true );
                                    if ( $manage_stock == "no" ) {                                        
                                            $payment_method = $order->get_payment_method();                                        
                                            if ( $payment_method == "cheque" ) {
                                                    $cancel_order = False;
                                            }
                                    }                                
                            }
                            if ( $cancel_order == True ) {
                                    $order -> update_status( 'cancelled', __( 'Pagamento não identificado e cancelado.', 'woocommerce') );
                            }
                    }
            }        
    }
    add_action( 'woocommerce_cancel_unpaid_submitted', 'wc_cancel_unpaid_submitted' );
    /* End of code. */

Trying to use this code in function.php, but not working.. Any idea whats wrong?

// To change the amount of days just change '-7 days' to your liking
function get_unpaid_submitted() {        
            global $wpdb;

            $unpaid_submitted = $wpdb->get_col( $wpdb->prepare( "
                    SELECT posts.ID
                    FROM {$wpdb->posts} AS posts
                    WHERE posts.post_status = 'wc-processing'
                    AND posts.post_date < %s
            ", date( 'Y-m-d H:i:s', strtotime('-14 days') ) ) );

            return $unpaid_submitted;
    }

    // This excludes check payment type.
    function wc_cancel_unpaid_submitted() {        
            $unpaid_submit = get_unpaid_submitted();

            if ( $unpaid_submit ) {                
                    foreach ( $unpaid_submit as $unpaid_order ) {                        
                            $order = wc_get_order( $unpaid_order );
                            $cancel_order = True;

                            foreach  ( $order->get_items() as $item_key => $item_values) {                                
                                    $manage_stock = get_post_meta( $item_values['variation_id'], '_manage_stock', true );
                                    if ( $manage_stock == "no" ) {                                        
                                            $payment_method = $order->get_payment_method();                                        
                                            if ( $payment_method == "cheque" ) {
                                                    $cancel_order = False;
                                            }
                                    }                                
                            }
                            if ( $cancel_order == True ) {
                                    $order -> update_status( 'cancelled', __( 'Pagamento não identificado e cancelado.', 'woocommerce') );
                            }
                    }
            }        
    }
    add_action( 'woocommerce_cancel_unpaid_submitted', 'wc_cancel_unpaid_submitted' );
    /* End of code. */
Share Improve this question edited Aug 15, 2020 at 12:22 rozklad 6321 gold badge6 silver badges15 bronze badges asked Aug 14, 2020 at 8:37 Morten HansenMorten Hansen 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You need to know that you can do it directly with a function in your functions.php file or a plugin, but it's not the best solution. For me the best solution is a cron job associate with your action because you wanna check each hour/day/week/or month the payment status of your orders. You don't want connect yourself on the website for the function work each time you want check this status.

(btw, sorry for my bad english)

For answer to your question, I see your code here: https://wordpress.org/support/topic/expiring-on-hold-cheque-orders-after-x-days/#post-10000589

I think this solution'll work only if you add the action to the cron job.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253403a121.html

最新回复(0)