ChatGPT generated plugin to track WooCommerce order status changes to a custom database table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: WooCommerce Order Status Tracker | |
* Plugin URI: https://example.com/woocommerce-order-status-tracker | |
* Description: This plugin tracks WooCommerce order status changes and logs them to the database. | |
* Version: 1.0.0 | |
* Author: Your Name | |
* Author URI: https://example.com | |
* License: GPL2 | |
*/ | |
class WC_Order_Status_Tracker { | |
/** | |
* Initializes the plugin. | |
* | |
* @return void | |
*/ | |
public function __construct() { | |
add_action( 'woocommerce_order_status_changed', array( $this, 'track_order_status_change' ), 10, 3 ); | |
} | |
/** | |
* Tracks an order status change. | |
* | |
* @param int $order_id The ID of the order. | |
* @param string $old_status The old order status. | |
* @param string $new_status The new order status. | |
* | |
* @return void | |
*/ | |
public function track_order_status_change( $order_id, $old_status, $new_status ) { | |
global $wpdb; | |
// Log the order status change to the database. | |
$wpdb->insert( | |
"{$wpdb->prefix}woocommerce_order_status_changes", | |
array( | |
'order_id' => $order_id, | |
'old_status' => $old_status, | |
'new_status' => $new_status, | |
'changed_at' => current_time( 'mysql' ), | |
) | |
); | |
} | |
} | |
new WC_Order_Status_Tracker(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment