Unverified

Name

Automatically Cancel WooCommerce Failed Orders Older Than One Week

About

This snippet automatically cancels WooCommerce orders that have a 'failed' status for more than one week. The process is run daily to keep your order management clean and up-to-date.

Language

PHP

Rating

Voted: 1 by 1 user(s)

How to Setup Snippet

To use this snippet, first, install and activate the Code Snippets plugin on your WordPress site. After activation, go to the Snippets section, click "Add New," and enter a title like "Automatically Cancel WooCommerce Failed Orders." Paste the provided code into the code editor, save the snippet, and activate it. The code will automatically schedule a daily check to cancel any failed orders older than one week.

Codevault

WPAccess

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Website/ Profile URL:

https://wpaccess.in/

Snippet Source:

https://wpaccess.in/

History

Last modified:

23/08/2024

Important Note

This snippet has the following status:

Unverified

This snippet has not been verified, use with caution and at your own risk. See details provided by author in sidebar and click below to find out more.

Automatically Cancel WooCommerce Failed Orders Older Than One Week

 
                    
1function disable_payment_for_failed_orders_after_one_week() {
2 // Get all failed orders
3 $failed_orders = wc_get_orders( array(
4 'limit' => -1,
5 'status' => 'failed',
6 'date_created' => '<' . ( time() - 7 * 24 * 60 * 60 ), // One week ago
7 ) );
8 
9 // Loop through each failed order and update its status to 'cancelled'
10 foreach ( $failed_orders as $order ) {
11 $order->update_status( 'cancelled', __( 'Payment failed after one week.', 'your-textdomain' ) );
12 }
13}
14 
15// Schedule the function to run once a day
16function schedule_disable_payment_for_failed_orders() {
17 if ( ! wp_next_scheduled( 'disable_payment_for_failed_orders' ) ) {
18 wp_schedule_event( time(), 'daily', 'disable_payment_for_failed_orders' );
19 }
20}
21add_action( 'wp', 'schedule_disable_payment_for_failed_orders' );
22 
23// Hook the function to run daily
24add_action( 'disable_payment_for_failed_orders', 'disable_payment_for_failed_orders_after_one_week' );

1

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

6

Delete Woocommerce images after deleting product

Added: 2 years ago

Last Updated: 1 week ago

Automatically Delete WooCommerce Images After Deleting a Product. If you use the same image of multiple products the image will not be deleted for those

WooCommerce

AI Verified

1

WooCommerce Change “$0.00” to “Free”

Added: 1 year ago

Last Updated: 10 months ago

If you have products which are free, the single product template and archive loop item will display the price as “$0.00” by default. This default display could confuse users. A better options would be...

WooCommerce

AI Verified

0

WooCommerce Move Product Category Description Below Product Loop

Added: 1 year ago

Last Updated: 1 year ago

This snippet will allow you to move your product description below the loop of products.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

Unverified

1

404 Redirection to Home page

Added: 3 weeks ago

Last Updated: 3 weeks ago

This snippet redirects all 404 errors to the homepage, providing a seamless user experience and preventing visitors from encountering dead ends

WooCommerce

Unverified

1

Add a Custom Prefix to WooCommerce Order Numbers

Added: 3 weeks ago

Last Updated: 3 weeks ago

This snippet adds a custom prefix 'ORDER-' to all WooCommerce order numbers, making it easy to customize and track your orders uniquely." If you need any further adjustments or additional snippets,...

WooCommerce

Unverified

1

Automatically Cancel WooCommerce Failed Orders Older Than One Week

Added: 3 weeks ago

Last Updated: 3 weeks ago

This snippet automatically cancels WooCommerce orders that have a 'failed' status for more than one week. The process is run daily to keep your order management clean and up-to-date.