-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathdynamically-change-woocommerce-order-date-on-status-change.code-snippets.xml
46 lines (46 loc) · 1.99 KB
/
dynamically-change-woocommerce-order-date-on-status-change.code-snippets.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a code snippets export file generated by the Code Snippets WordPress plugin. -->
<!-- https://wordpress.org/plugins/code-snippets -->
<!-- To import these snippets a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Install the Code Snippets plugin using the directions provided at the above link. -->
<!-- 3. Go to 'Tools: Import' in the WordPress admin panel. -->
<!-- 4. Click on the "Code Snippets" importer in the list -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. Code Snippets will then import all of the snippets and associated information contained in this file into your site. -->
<!-- 7. You will then have to visit the 'Snippets: All Snippets' admin menu and activate desired snippets. -->
<!-- generator="Code Snippets/2.9.4" created="2017-09-22 14:57" -->
<snippets>
<snippet scope="1">
<name>Dynamically change WooCommerce order date on status change</name>
<desc></desc>
<tags>order, status, woocommerce, change</tags>
<code>add_action( 'woocommerce_loaded', 'wc_custom_loaded' );
function wc_custom_loaded() {
$old_statuses = array(
'failed',
//uncomment any of the below statuses to include those statuses
//'pending',
//'processing',
//'on-hold',
//'cancelled',
//'refunded'
);
foreach ( $old_statuses as $old_status ) {
add_action( 'woocommerce_order_status_' . $old_status . '_to_completed', 'wc_custom_change_posted_date', 99, 1 );
}
}
function wc_custom_change_posted_date( $order_id ) {
$order = wc_get_order( $order_id );
$args = array(
'post_id' => $order_id,
//wp_insert_post (called by wp_update_post) will set the date to "now" if `post_date` is empty, likewise with `post_date_gmt`
'post_date' => '',
'post_date_gmt' => '',
);
wp_update_post( $args );
}</code>
</snippet>
</snippets>