Browse Source

add studiou-wc-ord-print-statuses plugin

Dalibor Votruba 1 year ago
commit
4337b60da2
1 changed files with 210 additions and 0 deletions
  1. 210 0
      studiou-wc-ord-print-statuses/studiou-wc-ord-print-statuses.php

+ 210 - 0
studiou-wc-ord-print-statuses/studiou-wc-ord-print-statuses.php

@@ -0,0 +1,210 @@
+<?php
+/**
+ * Plugin Name: QDR - Studiou WC Order Print Statuses
+ * Plugin URI: https://www.quadarax.com/plugins/studiou-wc-ord-print-statuses
+ * Description: Adds custom order statuses (to-print,in-print), bulk actions, and status change timestamps to WooCommerce for print orders to third party provider
+ * Version: 1.0
+ * Author: Dalibor Votruba
+ * Author URI: https://www.quadarax.com
+ * License: GPL2
+ */
+
+// Exit if accessed directly
+if (!defined('ABSPATH')) {
+    exit;
+}
+
+class Studiou_WC_Order_Print_Statuses {
+    /**
+     * Constructor: Sets up the necessary action and filter hooks
+     */
+    public function __construct() {
+        // Hook for registering custom order status
+        add_action('init', array($this, 'register_custom_order_status'));
+
+        // Filter to add custom status to WooCommerce order statuses list
+        add_filter('wc_order_statuses', array($this, 'add_to_print_order_status_to_list'));
+
+        // Filter to add custom status to WooCommerce order statuses list
+        add_filter('wc_order_statuses', array($this, 'add_in_print_order_status_to_list'));
+
+        // Filter to add custom status to bulk actions dropdown
+        add_filter('bulk_actions-edit-shop_order', array($this, 'add_to_print_order_status_to_bulk_actions'));
+        
+        // Filter to add custom status to bulk actions dropdown
+        add_filter('bulk_actions-edit-shop_order', array($this, 'add_in_print_order_status_to_bulk_actions'));
+
+        // Filter to handle the custom bulk action
+        add_filter('handle_bulk_actions-edit-shop_order', array($this, 'handle_custom_order_status_bulk_action'), 10, 3);
+        
+        // Action to add timestamp when order status changes
+        add_action('woocommerce_order_status_changed', array($this, 'add_status_change_timestamp'), 10, 4);
+        
+        // Filter to add custom column to orders table
+        add_filter('manage_edit-shop_order_columns', array($this, 'add_order_status_changed_column'));
+
+        // Action to populate the custom column
+        add_action('manage_shop_order_posts_custom_column', array($this, 'populate_order_status_changed_column'), 10, 2);
+    }
+
+     /**
+     * Registers the custom order status with WordPress
+     */
+    public function register_custom_order_status() {
+        register_post_status('wc-to-print', array(
+            'label' => 'Prepare to Printing',
+            'public' => true,
+            'exclude_from_search' => false,
+            'show_in_admin_all_list' => true,
+            'show_in_admin_status_list' => true,
+            'label_count' => _n_noop('Prepare to Printing <span class="count">(%s)</span>', 'Prepare to Printing <span class="count">(%s)</span>')
+        ));
+        register_post_status('wc-in-print', array(
+            'label' => 'In Printing',
+            'public' => true,
+            'exclude_from_search' => false,
+            'show_in_admin_all_list' => true,
+            'show_in_admin_status_list' => true,
+            'label_count' => _n_noop('In Printing <span class="count">(%s)</span>', 'In Printing <span class="count">(%s)</span>')
+        ));
+    }
+
+    /**
+     * Adds the wc-to-print order status to the list of WooCommerce order statuses
+     * 
+     * @param array $order_statuses Existing order statuses
+     * @return array Modified order statuses
+     */
+    public function add_to_print_order_status_to_list($order_statuses) {
+        $new_order_statuses = array();
+
+        // Add the custom status after the "Processing" status
+        foreach ($order_statuses as $key => $status) {
+            $new_order_statuses[$key] = $status;
+            if ($key === 'wc-processing') {
+                $new_order_statuses['wc-to-print'] = 'Prepare to Printing';
+            }
+        }
+
+        return $new_order_statuses;
+    }
+
+    /**
+     * Adds the wc-in-print order status to the list of WooCommerce order statuses
+     * 
+     * @param array $order_statuses Existing order statuses
+     * @return array Modified order statuses
+     */
+    public function add_in_print_order_status_to_list($order_statuses) {
+        $new_order_statuses = array();
+
+        // Add the custom status after the "Processing" status
+        foreach ($order_statuses as $key => $status) {
+            $new_order_statuses[$key] = $status;
+            if ($key === 'wc-to-print') {
+                $new_order_statuses['wc-in-print'] = 'In Printing';
+            }
+        }
+
+        return $new_order_statuses;
+    }
+
+    /**
+     * Adds the wc-to-print order status to the bulk actions dropdown
+     * 
+     * @param array $bulk_actions Existing bulk actions
+     * @return array Modified bulk actions
+     */
+    public function add_to_print_order_status_to_bulk_actions($bulk_actions) {
+        $bulk_actions['mark_to_print'] = 'Change status to Prepare to Printing';
+        return $bulk_actions;
+    }
+
+    /**
+     * Adds the wc-in-print order status to the bulk actions dropdown
+     * 
+     * @param array $bulk_actions Existing bulk actions
+     * @return array Modified bulk actions
+     */
+    public function add_in_print_order_status_to_bulk_actions($bulk_actions) {
+        $bulk_actions['mark_in_print'] = 'Change status to In Printing';
+        return $bulk_actions;
+    }
+
+ends here
+
+
+    /**
+     * Handles the custom order status bulk action
+     * 
+     * @param string $redirect_to The redirect URL
+     * @param string $action The bulk action being taken
+     * @param array $post_ids The IDs of the affected orders
+     * @return string The modified redirect URL
+     */
+    public function handle_custom_order_status_bulk_action($redirect_to, $action, $post_ids) {
+        if ($action !== 'mark_custom-status') {
+            return $redirect_to;
+        }
+
+        foreach ($post_ids as $post_id) {
+            $order = wc_get_order($post_id);
+            $order->update_status('custom-status', 'Order status changed by bulk edit:', true);
+        }
+
+        $redirect_to = add_query_arg('bulk_custom_status_orders_count', count($post_ids), $redirect_to);
+        return $redirect_to;
+    }
+
+    /**
+     * Adds a timestamp meta data when an order status is changed
+     * 
+     * @param int $order_id The ID of the order being changed
+     * @param string $old_status The old status of the order
+     * @param string $new_status The new status of the order
+     * @param WC_Order $order The order object
+     */
+    public function add_status_change_timestamp($order_id, $old_status, $new_status, $order) {
+        $timestamp = current_time('mysql');
+        update_post_meta($order_id, '_status_changed_timestamp', $timestamp);
+    }
+
+    /**
+     * Adds a new column to the orders table for the status change timestamp
+     * 
+     * @param array $columns Existing columns in the orders table
+     * @return array Modified columns
+     */
+    public function add_order_status_changed_column($columns) {
+        $new_columns = array();
+
+        foreach ($columns as $column_name => $column_info) {
+            $new_columns[$column_name] = $column_info;
+            if ($column_name === 'order_status') {
+                $new_columns['order_status_changed'] = __('Status Changed', 'woocommerce');
+            }
+        }
+
+        return $new_columns;
+    }
+
+    /**
+     * Populates the custom column with the status change timestamp
+     * 
+     * @param string $column The name of the column to populate
+     * @param int $post_id The ID of the order
+     */
+    public function populate_order_status_changed_column($column, $post_id) {
+        if ($column == 'order_status_changed') {
+            $timestamp = get_post_meta($post_id, '_status_changed_timestamp', true);
+            if ($timestamp) {
+                echo date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($timestamp));
+            } else {
+                echo '—';
+            }
+        }
+    }
+}
+
+// Instantiate the plugin class
+new Studiou_WC_Order_Print_Statuses();