Plugin Directory

Changeset 3427631


Ignore:
Timestamp:
12/26/2025 10:07:01 AM (2 months ago)
Author:
alexhalfborg
Message:

Version 1.0.1 - Fixed HPOS compatibility

Location:
pmor-reports/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • pmor-reports/trunk/includes/abstract-pmor-reports-report.php

    r3270750 r3427631  
    1616 */
    1717abstract class PMOR_Reports_Abstract_Report implements PMOR_Reports_Interface_Report {
     18    /**
     19     * Check if HPOS (High-Performance Order Storage) is enabled.
     20     *
     21     * @return bool
     22     */
     23    protected function is_hpos_enabled() {
     24        if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) ) {
     25            return \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
     26        }
     27        return false;
     28    }
     29
    1830    /**
    1931     * Get date ranges.
     
    7890        global $wpdb;
    7991
    80         $date_range = $this->get_date_range_dates( 
     92        $date_range = $this->get_date_range_dates(
    8193            isset( $args['date_range'] ) ? $args['date_range'] : '7day',
    8294            isset( $args['start_date'] ) ? $args['start_date'] : '',
     
    8496        );
    8597
    86         // Directly query for all payment methods within the date range
    87         $results = $wpdb->get_results(
    88             $wpdb->prepare(
    89                 "SELECT DISTINCT pm.meta_value as payment_method
    90                 FROM {$wpdb->postmeta} as pm
    91                 JOIN {$wpdb->posts} as p ON p.ID = pm.post_id
    92                 WHERE pm.meta_key = '_payment_method'
    93                 AND p.post_type = 'shop_order'
    94                 AND p.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
    95                 AND p.post_date >= %s
    96                 AND p.post_date <= %s
    97                 AND pm.meta_value != ''
    98                 ORDER BY pm.meta_value ASC",
    99                 gmdate( 'Y-m-d H:i:s', $date_range['start'] ),
    100                 gmdate( 'Y-m-d H:i:s', $date_range['end'] )
    101             )
    102         );
    10398        $payment_methods = array();
    104        
    105         if ( $results ) {
    106             foreach ( $results as $result ) {
    107                 // Use the raw payment method slug as both key and value
    108                 $payment_methods[ $result->payment_method ] = $result->payment_method;
     99
     100        if ( $this->is_hpos_enabled() ) {
     101            // HPOS mode: Query the wc_orders table directly.
     102            $orders_table = $wpdb->prefix . 'wc_orders';
     103
     104            $results = $wpdb->get_results(
     105                $wpdb->prepare(
     106                    "SELECT DISTINCT payment_method
     107                    FROM {$orders_table}
     108                    WHERE type = 'shop_order'
     109                    AND status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
     110                    AND date_created_gmt >= %s
     111                    AND date_created_gmt <= %s
     112                    AND payment_method != ''
     113                    ORDER BY payment_method ASC",
     114                    gmdate( 'Y-m-d H:i:s', $date_range['start'] ),
     115                    gmdate( 'Y-m-d H:i:s', $date_range['end'] )
     116                )
     117            );
     118
     119            if ( $results ) {
     120                foreach ( $results as $result ) {
     121                    $payment_methods[ $result->payment_method ] = $result->payment_method;
     122                }
    109123            }
    110         }
    111        
    112         // Check for orders with missing payment methods
    113         $missing_count = $wpdb->get_var(
    114             $wpdb->prepare(
    115                 "SELECT COUNT(p.ID) as count
    116                 FROM {$wpdb->posts} as p
    117                 LEFT JOIN {$wpdb->postmeta} as pm ON p.ID = pm.post_id AND pm.meta_key = '_payment_method'
    118                 WHERE p.post_type = 'shop_order'
    119                 AND p.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
    120                 AND p.post_date >= %s
    121                 AND p.post_date <= %s
    122                 AND (pm.meta_value IS NULL OR pm.meta_value = '')",
    123                 gmdate( 'Y-m-d H:i:s', $date_range['start'] ),
    124                 gmdate( 'Y-m-d H:i:s', $date_range['end'] )
    125             )
    126         );
    127        
     124
     125            // Check for orders with missing payment methods.
     126            $missing_count = $wpdb->get_var(
     127                $wpdb->prepare(
     128                    "SELECT COUNT(id)
     129                    FROM {$orders_table}
     130                    WHERE type = 'shop_order'
     131                    AND status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
     132                    AND date_created_gmt >= %s
     133                    AND date_created_gmt <= %s
     134                    AND (payment_method IS NULL OR payment_method = '')",
     135                    gmdate( 'Y-m-d H:i:s', $date_range['start'] ),
     136                    gmdate( 'Y-m-d H:i:s', $date_range['end'] )
     137                )
     138            );
     139        } else {
     140            // Legacy mode: Query wp_posts and wp_postmeta tables.
     141            $results = $wpdb->get_results(
     142                $wpdb->prepare(
     143                    "SELECT DISTINCT pm.meta_value as payment_method
     144                    FROM {$wpdb->postmeta} as pm
     145                    JOIN {$wpdb->posts} as p ON p.ID = pm.post_id
     146                    WHERE pm.meta_key = '_payment_method'
     147                    AND p.post_type = 'shop_order'
     148                    AND p.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
     149                    AND p.post_date >= %s
     150                    AND p.post_date <= %s
     151                    AND pm.meta_value != ''
     152                    ORDER BY pm.meta_value ASC",
     153                    gmdate( 'Y-m-d H:i:s', $date_range['start'] ),
     154                    gmdate( 'Y-m-d H:i:s', $date_range['end'] )
     155                )
     156            );
     157
     158            if ( $results ) {
     159                foreach ( $results as $result ) {
     160                    $payment_methods[ $result->payment_method ] = $result->payment_method;
     161                }
     162            }
     163
     164            // Check for orders with missing payment methods.
     165            $missing_count = $wpdb->get_var(
     166                $wpdb->prepare(
     167                    "SELECT COUNT(p.ID) as count
     168                    FROM {$wpdb->posts} as p
     169                    LEFT JOIN {$wpdb->postmeta} as pm ON p.ID = pm.post_id AND pm.meta_key = '_payment_method'
     170                    WHERE p.post_type = 'shop_order'
     171                    AND p.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
     172                    AND p.post_date >= %s
     173                    AND p.post_date <= %s
     174                    AND (pm.meta_value IS NULL OR pm.meta_value = '')",
     175                    gmdate( 'Y-m-d H:i:s', $date_range['start'] ),
     176                    gmdate( 'Y-m-d H:i:s', $date_range['end'] )
     177                )
     178            );
     179        }
     180
    128181        if ( $missing_count > 0 ) {
    129182            $payment_methods['missing'] = 'missing';
  • pmor-reports/trunk/includes/reports/class-pmor-report-payment-methods.php

    r3270750 r3427631  
    108108        global $wpdb;
    109109
    110         $date_range = $this->get_date_range_dates( 
     110        $date_range = $this->get_date_range_dates(
    111111            isset( $args['date_range'] ) ? $args['date_range'] : '7day',
    112112            isset( $args['start_date'] ) ? $args['start_date'] : '',
     
    114114        );
    115115
    116         // If this is an export, ignore payment method filter
     116        // If this is an export, ignore payment method filter.
    117117        $payment_method = isset( $args['export'] ) && $args['export'] ? '' : ( isset( $args['payment_method'] ) ? $args['payment_method'] : '' );
    118118
    119         // Prepare query arguments
     119        // Prepare query arguments.
    120120        $query_args = array(
    121121            gmdate( 'Y-m-d H:i:s', $date_range['start'] ),
     
    123123        );
    124124
    125         // Build base query
    126         $sql = "
    127             SELECT
    128                 posts.ID as order_id,
    129                 posts.post_date as order_date,
    130                 payment_method.meta_value as payment_method,
    131                 order_total.meta_value as order_total
    132             FROM
    133                 {$wpdb->posts} as posts
    134             LEFT JOIN
    135                 {$wpdb->postmeta} as payment_method ON posts.ID = payment_method.post_id AND payment_method.meta_key = '_payment_method'
    136             LEFT JOIN
    137                 {$wpdb->postmeta} as order_total ON posts.ID = order_total.post_id AND order_total.meta_key = '_order_total'
    138             WHERE
    139                 posts.post_type = 'shop_order'
    140                 AND posts.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
    141                 AND posts.post_date >= %s
    142                 AND posts.post_date <= %s
    143         ";
    144 
    145         // Add payment method filter if specified
    146         if ( ! empty( $payment_method ) ) {
    147             if ( $payment_method === 'missing' ) {
    148                 $sql .= " AND (payment_method.meta_value IS NULL OR payment_method.meta_value = '')";
    149             } else {
    150                 $sql .= " AND payment_method.meta_value = %s";
    151                 $query_args[] = $payment_method;
     125        if ( $this->is_hpos_enabled() ) {
     126            // HPOS mode: Query the wc_orders table directly.
     127            $orders_table = $wpdb->prefix . 'wc_orders';
     128
     129            $sql = "
     130                SELECT
     131                    o.id as order_id,
     132                    o.date_created_gmt as order_date,
     133                    o.payment_method as payment_method,
     134                    o.total_amount as order_total
     135                FROM {$orders_table} as o
     136                WHERE o.type = 'shop_order'
     137                AND o.status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
     138                AND o.date_created_gmt >= %s
     139                AND o.date_created_gmt <= %s
     140            ";
     141
     142            // Add payment method filter if specified.
     143            if ( ! empty( $payment_method ) ) {
     144                if ( $payment_method === 'missing' ) {
     145                    $sql .= " AND (o.payment_method IS NULL OR o.payment_method = '')";
     146                } else {
     147                    $sql .= " AND o.payment_method = %s";
     148                    $query_args[] = $payment_method;
     149                }
    152150            }
    153         }
    154 
    155         // Add order by clause
    156         $sql .= " ORDER BY posts.post_date DESC";
    157 
    158         // Prepare the SQL query with all placeholders
     151
     152            // Add order by clause.
     153            $sql .= " ORDER BY o.date_created_gmt DESC";
     154        } else {
     155            // Legacy mode: Query wp_posts and wp_postmeta tables.
     156            $sql = "
     157                SELECT
     158                    posts.ID as order_id,
     159                    posts.post_date as order_date,
     160                    payment_method.meta_value as payment_method,
     161                    order_total.meta_value as order_total
     162                FROM
     163                    {$wpdb->posts} as posts
     164                LEFT JOIN
     165                    {$wpdb->postmeta} as payment_method ON posts.ID = payment_method.post_id AND payment_method.meta_key = '_payment_method'
     166                LEFT JOIN
     167                    {$wpdb->postmeta} as order_total ON posts.ID = order_total.post_id AND order_total.meta_key = '_order_total'
     168                WHERE
     169                    posts.post_type = 'shop_order'
     170                    AND posts.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
     171                    AND posts.post_date >= %s
     172                    AND posts.post_date <= %s
     173            ";
     174
     175            // Add payment method filter if specified.
     176            if ( ! empty( $payment_method ) ) {
     177                if ( $payment_method === 'missing' ) {
     178                    $sql .= " AND (payment_method.meta_value IS NULL OR payment_method.meta_value = '')";
     179                } else {
     180                    $sql .= " AND payment_method.meta_value = %s";
     181                    $query_args[] = $payment_method;
     182                }
     183            }
     184
     185            // Add order by clause.
     186            $sql .= " ORDER BY posts.post_date DESC";
     187        }
     188
     189        // Prepare the SQL query with all placeholders.
    159190        $prepared_sql = $wpdb->prepare( $sql, $query_args );
    160191
    161         // Execute the prepared query
     192        // Execute the prepared query.
    162193        $results = $wpdb->get_results( $prepared_sql );
    163194
     
    166197        if ( $results ) {
    167198            foreach ( $results as $result ) {
    168                 // Use the raw payment method slug for display
    169                 $payment_method_display = empty($result->payment_method) ? esc_html__( 'Unknown/Missing Payment Method', 'pmor-reports' ) : $result->payment_method;
    170                
     199                // Use the raw payment method slug for display.
     200                $payment_method_display = empty( $result->payment_method ) ? esc_html__( 'Unknown/Missing Payment Method', 'pmor-reports' ) : $result->payment_method;
     201
    171202                $data[] = array(
    172203                    'order_id'       => $result->order_id,
     
    182213        }
    183214
    184         // Store total count for pagination
     215        // Store total count for pagination.
    185216        $this->total_items = count( $data );
    186        
    187         // If not exporting and not skipping pagination, apply pagination
     217
     218        // If not exporting and not skipping pagination, apply pagination.
    188219        if ( ( ! isset( $args['export'] ) || ! $args['export'] ) && ( ! isset( $args['no_pagination'] ) || ! $args['no_pagination'] ) ) {
    189             // Get current page
     220            // Get current page.
    190221            $current_page = isset( $args['paged'] ) ? absint( $args['paged'] ) : 1;
    191             $per_page = 20; // Items per page
    192            
    193             // Calculate offset
     222            $per_page     = 20; // Items per page.
     223
     224            // Calculate offset.
    194225            $offset = ( $current_page - 1 ) * $per_page;
    195            
    196             // Slice the data array for pagination
     226
     227            // Slice the data array for pagination.
    197228            $data = array_slice( $data, $offset, $per_page );
    198229        }
  • pmor-reports/trunk/pmor-reports.php

    r3270750 r3427631  
    44 * Plugin URI: https://halfborg.com/pmor-reports
    55 * Description: Advanced reporting for WooCommerce orders with filtering by payment method and date ranges.
    6  * Version: 1.0.0
     6 * Version: 1.0.1
    77 * Author: Alex Halfborg
    88 * Author URI: https://halfborg.com
     
    2626
    2727// Define plugin constants.
    28 define( 'PMOR_REPORTS_VERSION', '1.0.0' );
     28define( 'PMOR_REPORTS_VERSION', '1.0.1' );
    2929define( 'PMOR_REPORTS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    3030define( 'PMOR_REPORTS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
  • pmor-reports/trunk/readme.txt

    r3270750 r3427631  
    55Tested up to: 6.7
    66Requires PHP: 7.2
    7 Stable tag: 1.0.0
     7Stable tag: 1.0.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    5353== Changelog ==
    5454
     55= 1.0.1 =
     56* Fixed HPOS (High-Performance Order Storage) compatibility - plugin now correctly queries orders from WooCommerce's new order tables when HPOS is enabled
     57
    5558= 1.0.0 =
    5659* Initial release
     
    5861== Upgrade Notice ==
    5962
     63= 1.0.1 =
     64Important fix for HPOS compatibility. If you're using WooCommerce's High-Performance Order Storage and not seeing recent orders, update immediately.
     65
    6066= 1.0.0 =
    6167Initial release
Note: See TracChangeset for help on using the changeset viewer.