Skip to content

Instantly share code, notes, and snippets.

@billrobbins
Created December 8, 2022 14:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save billrobbins/721c88780e1a9600b45689eef947186b to your computer and use it in GitHub Desktop.
ChatGPT generated plugin to track WooCommerce order status changes to a custom database table.
<?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