| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- <?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
- */
- defined('ABSPATH') or die('Direct access not allowed!');
- /**
- * Main plugin class
- */
- class Studiou_WC_Ord_Print_Statuses {
- /**
- * Constructor
- */
- public function __construct() {
- add_action('plugins_loaded', array($this, 'init'));
- }
- /**
- * Initialize the plugin
- */
- public function init() {
- // Check if WooCommerce is active
- if (!class_exists('WooCommerce')) {
- add_action('admin_notices', array($this, 'woocommerce_missing_notice'));
- return;
- }
- // Initialize plugin features
- $this->add_custom_order_statuses();
- $this->add_custom_order_fields();
- $this->modify_order_search();
- $this->add_bulk_actions();
- $this->add_custom_columns();
- $this->add_import_buttons();
- }
- /**
- * Display a notice if WooCommerce is not active
- */
- public function woocommerce_missing_notice() {
- ?>
- <div class="error">
- <p><?php _e('Studiou WC Order Print Statuses requires WooCommerce to be installed and active.', 'studiou-wc-ord-print-statuses'); ?></p>
- </div>
- <?php
- }
- /**
- * Add custom order statuses
- */
- private function add_custom_order_statuses() {
- add_action('init', array($this, 'register_custom_order_statuses'));
- add_filter('wc_order_statuses', array($this, 'add_custom_order_statuses_to_list'));
- }
- /**
- * Register custom order statuses
- */
- public function register_custom_order_statuses() {
- register_post_status('wc-to-print', array(
- 'label' => _x('Prepare to Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
- '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' => _x('In Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
- '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>')
- ));
- }
- /**
- * Add custom order statuses to the list of WooCommerce order statuses
- */
- public function add_custom_order_statuses_to_list($order_statuses) {
- $new_order_statuses = array();
- foreach ($order_statuses as $key => $status) {
- $new_order_statuses[$key] = $status;
- if ($key === 'wc-processing') {
- $new_order_statuses['wc-to-print'] = _x('Prepare to Printing', 'Order status', 'studiou-wc-ord-print-statuses');
- $new_order_statuses['wc-in-print'] = _x('In Printing', 'Order status', 'studiou-wc-ord-print-statuses');
- }
- }
- return $new_order_statuses;
- }
- /**
- * Add custom order fields
- */
- private function add_custom_order_fields() {
- add_action('woocommerce_admin_order_data_after_order_details', array($this, 'display_custom_order_fields'));
- add_action('woocommerce_process_shop_order_meta', array($this, 'save_custom_order_fields'));
- }
- /**
- * Display custom order fields in the order edit page
- */
- public function display_custom_order_fields($order) {
- ?>
- <div class="order_data_column">
- <h4><?php _e('Print Information', 'studiou-wc-ord-print-statuses'); ?></h4>
- <p>
- <label for="to_print_date"><?php _e('Prepare To Print Date:', 'studiou-wc-ord-print-statuses'); ?></label>
- <input type="text" class="date-picker" name="to_print_date" id="to_print_date" value="<?php echo esc_attr($order->get_meta('to_print_date')); ?>" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])">
- </p>
- <p>
- <label for="in_print_date"><?php _e('In Print Date:', 'studiou-wc-ord-print-statuses'); ?></label>
- <input type="text" class="date-picker" name="in_print_date" id="in_print_date" value="<?php echo esc_attr($order->get_meta('in_print_date')); ?>" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])">
- </p>
- <p>
- <label for="external_ref_ord_no"><?php _e('External Order Number:', 'studiou-wc-ord-print-statuses'); ?></label>
- <input type="text" name="external_ref_ord_no" id="external_ref_ord_no" value="<?php echo esc_attr($order->get_meta('external_ref_ord_no')); ?>">
- </p>
- <p>
- <label for="external_ref_ord_delivered"><?php _e('External Order Delivered:', 'studiou-wc-ord-print-statuses'); ?></label>
- <input type="text" class="date-picker" name="external_ref_ord_delivered" id="external_ref_ord_delivered" value="<?php echo esc_attr($order->get_meta('external_ref_ord_delivered')); ?>" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])">
- </p>
- </div>
- <?php
- }
- /**
- * Save custom order fields
- */
- public function save_custom_order_fields($order_id) {
- $order = wc_get_order($order_id);
- if (isset($_POST['to_print_date'])) {
- $order->update_meta_data('to_print_date', sanitize_text_field($_POST['to_print_date']));
- }
- if (isset($_POST['in_print_date'])) {
- $order->update_meta_data('in_print_date', sanitize_text_field($_POST['in_print_date']));
- }
- if (isset($_POST['external_ref_ord_no'])) {
- $order->update_meta_data('external_ref_ord_no', sanitize_text_field($_POST['external_ref_ord_no']));
- }
- if (isset($_POST['external_ref_ord_delivered'])) {
- $order->update_meta_data('external_ref_ord_delivered', sanitize_text_field($_POST['external_ref_ord_delivered']));
- }
- $order->save();
- }
- /**
- * Modify order search to include external reference order number
- */
- private function modify_order_search() {
- add_filter('woocommerce_shop_order_search_fields', array($this, 'add_external_ref_ord_no_to_search'));
- }
- /**
- * Add external reference order number to search fields
- */
- public function add_external_ref_ord_no_to_search($search_fields) {
- $search_fields[] = 'external_ref_ord_no';
- return $search_fields;
- }
- /**
- * Add bulk actions to the orders list
- */
- private function add_bulk_actions() {
- add_filter('bulk_actions-edit-shop_order', array($this, 'register_bulk_actions'));
- add_filter('handle_bulk_actions-edit-shop_order', array($this, 'handle_bulk_actions'), 10, 3);
- }
- /**
- * Register custom bulk actions
- */
- public function register_bulk_actions($bulk_actions) {
- $bulk_actions['prepare_to_printing_export'] = __('Prepare to Printing Export', 'studiou-wc-ord-print-statuses');
- $bulk_actions['set_status_to_prepare_to_printing'] = __('Set Status to Prepare to Printing', 'studiou-wc-ord-print-statuses');
- $bulk_actions['set_status_to_in_printing'] = __('Set Status to In Printing', 'studiou-wc-ord-print-statuses');
- return $bulk_actions;
- }
- /**
- * Handle custom bulk actions
- */
- public function handle_bulk_actions($redirect_to, $action, $post_ids) {
- if ($action === 'prepare_to_printing_export') {
- $this->prepare_to_printing_export($post_ids);
- } elseif ($action === 'set_status_to_prepare_to_printing') {
- $this->set_status_to_prepare_to_printing($post_ids);
- } elseif ($action === 'set_status_to_in_printing') {
- $this->set_status_to_in_printing($post_ids);
- }
- return $redirect_to;
- }
- /**
- * Handle the "Prepare to Printing Export" bulk action
- */
- private function prepare_to_printing_export($post_ids) {
- global $wpdb;
- $results = $wpdb->get_results("SELECT * FROM `vsu_orders_car_prod_qty_img` WHERE order_id IN (" . implode(',', array_map('intval', $post_ids)) . ")");
-
- // Generate CSV file
- $filename = 'prepare_to_printing_export_' . date('Y-m-d_His') . '.csv';
- header('Content-Type: text/csv');
- header('Content-Disposition: attachment; filename="' . $filename . '"');
-
- $output = fopen('php://output', 'w');
- fputcsv($output, array_keys((array)$results[0])); // CSV header
-
- foreach ($results as $row) {
- fputcsv($output, (array)$row);
- }
-
- fclose($output);
-
- // Update order statuses and to-print-date
- foreach ($post_ids as $post_id) {
- $order = wc_get_order($post_id);
- $order->update_status('wc-to-print');
- $order->update_meta_data('to_print_date', current_time('mysql'));
- $order->save();
- }
-
- exit();
- }
- /**
- * Handle the "Set Status to Prepare to Printing" bulk action
- */
- private function set_status_to_prepare_to_printing($post_ids) {
- foreach ($post_ids as $post_id) {
- $order = wc_get_order($post_id);
- $order->update_status('wc-to-print');
- $order->update_meta_data('to_print_date', current_time('mysql'));
- $order->save();
- }
- }
- /**
- * Handle the "Set Status to In Printing" bulk action
- */
- private function set_status_to_in_printing($post_ids) {
- foreach ($post_ids as $post_id) {
- $order = wc_get_order($post_id);
- $order->update_status('wc-in-print');
- $order->update_meta_data('in_print_date', current_time('mysql'));
- $order->save();
- }
- }
- /**
- * Add custom columns to the orders list
- */
- private function add_custom_columns() {
- add_filter('manage_edit-shop_order_columns', array($this, 'add_custom_shop_order_column'), 20);
- add_action('manage_shop_order_posts_custom_column', array($this, 'add_custom_shop_order_column_content'), 20, 2);
- }
- /**
- * Add custom columns to the orders list table
- */
- public function add_custom_shop_order_column($columns) {
- $new_columns = array();
- foreach ($columns as $column_name => $column_info) {
- $new_columns[$column_name] = $column_info;
- if ('order_total' === $column_name) {
- $new_columns['to_print_date'] = __('To Print Date', 'studiou-wc-ord-print-statuses');
- $new_columns['in_print_date'] = __('In Print Date', 'studiou-wc-ord-print-statuses');
- $new_columns['external_ref_ord_no'] = __('External Order Number', 'studiou-wc-ord-print-statuses');
- }
- }
- return $new_columns;
- }
- /**
- * Add content to custom columns in the orders list table
- */
- public function add_custom_shop_order_column_content($column, $post_id) {
- $order = wc_get_order($post_id);
- switch ($column) {
- case 'to_print_date':
- echo esc_html($order->get_meta('to_print_date'));
- break;
- case 'in_print_date':
- echo esc_html($order->get_meta('in_print_date'));
- break;
- case 'external_ref_ord_no':
- echo esc_html($order->get_meta('external_ref_ord_no'));
- break;
- }
- }
- /**
- * Add import buttons to the orders page
- */
- private function add_import_buttons() {
- add_action('woocommerce_order_list_table_restrict_manage_orders', array($this, 'add_import_buttons_to_orders_page'));
- add_action('admin_post_import_inprint_protocol', array($this, 'handle_import_inprint_protocol'));
- add_action('admin_post_import_delivered_protocol', array($this, 'handle_import_delivered_protocol'));
- }
- /**
- * Add import buttons to the orders page
- */
- public function add_import_buttons_to_orders_page() {
- ?>
- <div class="alignleft actions">
- <button type="button" class="button import-inprint-protocol"><?php _e('Import InPrint Protocol', 'studiou-wc-ord-print-statuses'); ?></button>
- <button type="button" class="button import-delivered-protocol"><?php _e('Import Delivered Protocol', 'studiou-wc-ord-print-statuses'); ?></button>
- </div>
- <script>
- jQuery(function($) {
- // Move our buttons after the "Add Order" button
- var $addOrderButton = $('.page-title-action').first();
- $('.import-inprint-protocol, .import-delivered-protocol').insertAfter($addOrderButton);
- $('.import-inprint-protocol').click(function() {
- var fileInput = $('<input type="file" accept=".csv" style="display:none;">');
- fileInput.appendTo('body').trigger('click');
- fileInput.on('change', function() {
- var formData = new FormData();
- formData.append('action', 'import_inprint_protocol');
- formData.append('file', this.files[0]);
-
- $.ajax({
- url: '<?php echo admin_url('admin-post.php'); ?>',
- type: 'POST',
- data: formData,
- processData: false,
- contentType: false,
- success: function(response) {
- alert('InPrint Protocol import completed successfully.');
- location.reload();
- },
- error: function() {
- alert('InPrint Protocol import failed. Please try again.');
- }
- });
- });
- });
- $('.import-delivered-protocol').click(function() {
- var fileInput = $('<input type="file" accept=".csv" style="display:none;">');
- fileInput.appendTo('body').trigger('click');
- fileInput.on('change', function() {
- var formData = new FormData();
- formData.append('action', 'import_delivered_protocol');
- formData.append('file', this.files[0]);
-
- $.ajax({
- url: '<?php echo admin_url('admin-post.php'); ?>',
- type: 'POST',
- data: formData,
- processData: false,
- contentType: false,
- success: function(response) {
- alert('Delivered Protocol import completed successfully.');
- location.reload();
- },
- error: function() {
- alert('Delivered Protocol import failed. Please try again.');
- }
- });
- });
- });
- });
- </script>
- <?php
- }
- /**
- * Handle the import of InPrint protocol
- */
- public function handle_import_inprint_protocol() {
- if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
- wp_die('File upload failed.');
- }
- $file = fopen($_FILES['file']['tmp_name'], 'r');
- if ($file === false) {
- wp_die('Failed to open the uploaded file.');
- }
- // Skip the header row
- fgetcsv($file);
- while (($data = fgetcsv($file)) !== false) {
- $order_number = $data[0];
- $external_order = $data[1];
- $external_order_date = $data[2];
- $order = wc_get_order($order_number);
- if ($order) {
- $order->update_status('wc-in-print');
- $order->update_meta_data('in_print_date', current_time('mysql'));
- $order->update_meta_data('external_ref_ord_no', $external_order);
- $order->save();
- }
- }
- fclose($file);
- wp_redirect(admin_url('edit.php?post_type=shop_order'));
- exit;
- }
- /**
- * Handle the import of Delivered protocol
- */
- public function handle_import_delivered_protocol() {
- if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
- wp_die('File upload failed.');
- }
- $file = fopen($_FILES['file']['tmp_name'], 'r');
- if ($file === false) {
- wp_die('Failed to open the uploaded file.');
- }
- // Skip the header row
- fgetcsv($file);
- while (($data = fgetcsv($file)) !== false) {
- $order_number = $data[0];
- $order = wc_get_order($order_number);
- if ($order) {
- $order->update_status('wc-completed');
- $order->update_meta_data('external_ref_ord_delivered', current_time('mysql'));
- $order->save();
- }
- }
- fclose($file);
- wp_redirect(admin_url('edit.php?post_type=shop_order'));
- exit;
- }
- }
- // Initialize the plugin
- new Studiou_WC_Ord_Print_Statuses();
|