Răsfoiți Sursa

add loggings

Dalibor Votruba 1 an în urmă
părinte
comite
76e24211e9

+ 77 - 4
studiou-wc-ord-print-statuses/studiou-wc-ord-print-statuses.php

@@ -11,6 +11,7 @@
 
 defined('ABSPATH') or die('Direct access not allowed!');
 
+
 /**
  * Main plugin class
  */
@@ -21,18 +22,20 @@ class Studiou_WC_Ord_Print_Statuses {
      */
     public function __construct() {
         add_action('plugins_loaded', array($this, 'init'));
+        add_action('pre_get_posts', array($this, 'custom_columns_orderby'));
     }
 
     /**
      * Initialize the plugin
      */
     public function init() {
+        error_log('Plugin initializing...');
         // 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();
@@ -40,8 +43,28 @@ class Studiou_WC_Ord_Print_Statuses {
         $this->add_bulk_actions();
         $this->add_custom_columns();
         $this->add_import_buttons();
+        //$this->log_filter_names();
+        $this->log_woocommerce_filter_names();
+
+        // Log a debug message
+        error_log('Plugin initialized');
     }
 
+    function log_filter_names() {
+        global $wp_filter;
+        $filter_names = array_keys($wp_filter);
+        error_log('Current WordPress filter hooks: ' . print_r($filter_names, true));
+    }
+    function log_woocommerce_filter_names() {
+        global $wp_filter;
+        $all_filter_names = array_keys($wp_filter);
+        $woocommerce_filter_names = array_filter($all_filter_names, function($name) {
+            return (stripos($name, 'woocommerce') !== false) || (stripos($name, 'wc') !== false);
+        });
+        if (!empty($woocommerce_filter_names)) {
+            error_log('Current WooCommerce filter hooks: ' . print_r($woocommerce_filter_names, true));
+        }
+    }
     /**
      * Display a notice if WooCommerce is not active
      */
@@ -103,11 +126,13 @@ class Studiou_WC_Ord_Print_Statuses {
     }
 
     /**
-     * Add custom order fields
+     * Add custom order fields to order detail page
      */
     private function add_custom_order_fields() {
+        // to detail page
         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'));
+
     }
 
     /**
@@ -170,6 +195,7 @@ class Studiou_WC_Ord_Print_Statuses {
      * Add external reference order number to search fields
      */
     public function add_external_ref_ord_no_to_search($search_fields) {
+        error_log('add_external_ref_ord_no_to_search - called');
         $search_fields[] = 'external_ref_ord_no';
         return $search_fields;
     }
@@ -178,8 +204,9 @@ class Studiou_WC_Ord_Print_Statuses {
      * 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);
+        //add_filter('bulk_actions-edit-shop_order', array($this, 'register_bulk_actions'));
+        add_filter('bulk_actions-woocommerce_page_wc-orders', array($this, 'register_bulk_actions'));
+        add_filter('handle_bulk_actions-woocommerce_page_wc-orders', array($this, 'handle_bulk_actions'), 10, 3);
     }
 
     /**
@@ -269,6 +296,8 @@ class Studiou_WC_Ord_Print_Statuses {
     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_filter('manage_edit-shop_order_sortable_columns', array($this, 'make_custom_columns_sortable'));
+        add_filter('manage_woocommerce_page_wc-orders_custom_column', array($this, 'set_custom_columns_default_visibility'));
     }
 
     /**
@@ -284,11 +313,55 @@ class Studiou_WC_Ord_Print_Statuses {
                 $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');
+                error_log('add_custom_shop_order_column - columns add');
             }
+            error_log('add_custom_shop_order_column - finished');
         }
 
         return $new_columns;
     }
+
+    public function set_custom_columns_default_visibility($hidden) {
+        if (!is_array($hidden)) {
+            $hidden = array();
+        }
+    
+        // Remove our custom columns from the hidden list if they're there
+        $hidden = array_diff($hidden, array('to_print_date', 'in_print_date', 'external_ref_ord_no'));
+    
+        // Optionally, if you want these columns hidden by default, add them to the $hidden array
+        // $hidden[] = 'to_print_date';
+        // $hidden[] = 'in_print_date';
+        // $hidden[] = 'external_ref_ord_no';
+        error_log('set_custom_columns_default_visibility - finished');
+        return $hidden;
+    }
+
+    public function make_custom_columns_sortable($columns) {
+        $columns['to_print_date'] = 'to_print_date';
+        $columns['in_print_date'] = 'in_print_date';
+        $columns['external_ref_ord_no'] = 'external_ref_ord_no';
+        return $columns;
+    }
+
+    public function custom_columns_orderby($query) {
+        if (!is_admin() || !$query->is_main_query() || $query->get('post_type') !== 'shop_order') {
+            return;
+        }
+    
+        $orderby = $query->get('orderby');
+    
+        switch ($orderby) {
+            case 'to_print_date':
+            case 'in_print_date':
+            case 'external_ref_ord_no':
+                $query->set('meta_key', $orderby);
+                $query->set('orderby', 'meta_value');
+                break;
+        }
+    }
+
+
     /**
      * Add content to custom columns in the orders list table
      */