Skip to content

Instantly share code, notes, and snippets.

@BrianHenryIE
Created January 16, 2021 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianHenryIE/2f25ed9fef2f21f3536e343864681a7f to your computer and use it in GitHub Desktop.
Save BrianHenryIE/2f25ed9fef2f21f3536e343864681a7f to your computer and use it in GitHub Desktop.
diff --git a/woocommerce-gateway-stats.php b/woocommerce-gateway-stats.php
index e43cef5..e0726f6 100644
--- a/woocommerce-gateway-stats.php
+++ b/woocommerce-gateway-stats.php
@@ -5,7 +5,7 @@ Plugin URI: https://ajdg.solutions/product/woocommerce-gateway-stats/?pk_campaig
Author: Arnan de Gans
Author URI: https://www.arnan.me/?pk_campaign=woo_gateway_stats
Description: See which payment gateways are used the most. This plugin has no settings.
-Version: 1.0.7
+Version: 1.1.0
License: Limited License (See the readme.html in your account on https://ajdg.solutions/)
WC requires at least: 4.0
@@ -26,6 +26,8 @@ defined( 'ABSPATH' ) or die();
register_activation_hook( __FILE__, 'ajdg_gateway_activate' );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'ajdg_gateway_stats_action_links' );
+add_filter( 'plugin_row_meta', 'ajdg_gateway_stats_row_meta', 10, 4 );
+
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
add_filter( 'woocommerce_admin_reports', 'ajdg_gateway_stats_admin_reports', 10, 1 );
@@ -99,10 +101,32 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
public function get_main_chart() {
global $wpdb;
- $gateways = $wpdb->get_results(
+ $statuses = array_merge( wc_get_is_paid_statuses(), array( 'on-hold', 'refunded' ) );
+
+ $post_statuses = implode(
+ ' OR ',
+ array_map(
+ function( $status ) {
+ return "`post_status` = 'wc-{$status}'";
+ },
+ $statuses
+ )
+ );
+
+ /**
+ * The array of gateway data object returned from the database.
+ *
+ * @var stdClass[] $db_gateways {
+ * @var string gateway_id The id matching WC_Payment_Gateway::id.
+ * @var string title The gateway title - possibly an existing gateway, possibly one whose plugin has been removed.
+ * @var int amount The number of orders made using this gateway.
+ * @var float revenue The total money processed by this gateway.
+ * }
+ */
+ $db_gateways = $wpdb->get_results(
"
SELECT
- DISTINCT `{$wpdb->prefix}postmeta`.`meta_value` as `gateway`,
+ DISTINCT `{$wpdb->prefix}postmeta`.`meta_value` as `gateway_id`,
`meta_title`.`meta_value` as `title`,
COUNT(`{$wpdb->prefix}postmeta`.`meta_value`) as `amount`,
SUM(`meta_revenue`.`meta_value`) as `revenue`
@@ -118,20 +142,63 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
AND `{$wpdb->prefix}postmeta`.`meta_key` = '_payment_method'
AND `post_date` >= '" . date( 'Y-m-d H:i:s', $this->start_date ) . "'
AND `post_date` < '" . date( 'Y-m-d H:i:s', strtotime( '+1 DAY', $this->end_date ) ) . "'
- AND (`post_status` = 'wc-completed'
- OR `post_status` = 'wc-processing'
- OR `post_status` = 'wc-on-hold'
- OR `post_status` = 'wc-refunded')
+ AND ({$post_statuses})
GROUP BY `{$wpdb->prefix}postmeta`.`meta_value`
ORDER BY `amount` DESC;
"
);
+ /**
+ * Get the active gateways to use further information from.
+ *
+ * @var WC_Payment_Gateway[] $wc_gateways
+ */
+ $wc_gateways = WC()->payment_gateways()->payment_gateways();
+
$total_amount = $total_gateways = $total_revenue = 0;
- foreach ( $gateways as $gateway ) {
- $total_amount = $total_amount + $gateway->amount;
- $total_revenue = $total_revenue + $gateway->revenue;
+ foreach ( $db_gateways as $db_gateway ) {
+ $total_amount = $total_amount + $db_gateway->amount;
+ $total_revenue = $total_revenue + $db_gateway->revenue;
$total_gateways++;
+
+ if ( isset( $wc_gateways[ $db_gateway->gateway_id ] ) ) {
+ $wc_gateway = $wc_gateways[ $db_gateway->gateway_id ];
+ $gateway_admin_url = admin_url(
+ add_query_arg(
+ array(
+ 'page' => 'wc-settings',
+ 'tab' => 'checkout',
+ 'section' => $wc_gateway->id,
+ ),
+ '/admin.php'
+ )
+ );
+
+ $db_gateway->title = "<a target=\"_blank\" href=\"{$gateway_admin_url}\">{$wc_gateway->get_method_title()}</a>";
+
+ if ( $wc_gateway->get_method_title() !== $wc_gateway->get_title() ) {
+ $db_gateway->title .= " - {$wc_gateway->get_title()}";
+ }
+ } // else the gateway plugin no longer exists.
+
+ /**
+ * If SkyVerge's Filter WooCommerce Orders by Payment Method plugin is active, link the order counts
+ * to the list of orders for each gateway.
+ *
+ * @see https://gist.github.com/bekarice/41bce677437cb8f312ed77e9f226a812
+ */
+ if ( is_plugin_active( 'wc-filter-orders-by-payment/filter-wc-orders-by-gateway.php' ) || is_plugin_active( 'filter-wc-orders-by-gateway/filter-wc-orders-by-gateway.php' ) ) {
+ $orders_link = admin_url(
+ add_query_arg(
+ array(
+ 'post_type' => 'shop_order',
+ '_shop_order_payment_method' => $db_gateway->gateway_id,
+ ),
+ 'edit.php'
+ )
+ );
+ $db_gateway->amount = "<a target=\"_blank\" href =\"{$orders_link}\">$db_gateway->amount</a>";
+ }
}
?>
<table class="widefat">
@@ -145,12 +212,12 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
</thead>
<tbody>
- <?php foreach ( $gateways as $gateway ) { ?>
+ <?php foreach ( $db_gateways as $db_gateway ) { ?>
<tr>
- <td><?php echo ( $gateway->gateway != '' ) ? $gateway->title : '<span style="color: #aeaeae;">Not Selected/Unknown</span>'; ?></td>
- <td><center><?php echo wc_price( $gateway->revenue ); ?></center></td>
- <td><center><?php echo $gateway->amount; ?></center></td>
- <td><center><?php echo round( ( 100 / $total_amount ) * $gateway->amount, 2 ); ?>%</center></td>
+ <td><?php echo ( $db_gateway->gateway_id != '' ) ? $db_gateway->title : '<span style="color: #aeaeae;">Not Selected/Unknown</span>'; ?></td>
+ <td><center><?php echo wc_price( $db_gateway->revenue ); ?></center></td>
+ <td><center><?php echo $db_gateway->amount; ?></center></td>
+ <td><center><?php echo round( ( 100 / $total_amount ) * $db_gateway->amount, 2 ); ?>%</center></td>
</tr>
<?php } ?>
<tr>
@@ -186,9 +253,16 @@ function ajdg_gateway_activate() {
Since: 1.0
-------------------------------------------------------------*/
function ajdg_gateway_stats_action_links( $links ) {
- $links['ajdg-gateway-settings'] = sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=wc-reports&tab=orders&report=payment_gateway_usage' ), 'Stats' );
- $links['ajdg-gateway-help'] = sprintf( '<a href="%s" target="_blank">%s</a>', 'https://ajdg.solutions/forums/forum/woocommerce-plugins/?pk_campaign=woo_gateway_stats', 'Support' );
+ $link = array( 'ajdg-gateway-settings' => sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=wc-reports&tab=orders&report=payment_gateway_usage' ), 'View Report' ) );
+
+ return $link + $links;
+}
+function ajdg_gateway_stats_row_meta( $plugin_meta, $plugin_file_name, $plugin_data, $status ) {
+
+ if ( 'woocommerce-gateway-stats/woocommerce-gateway-stats.php' === $plugin_file_name ) {
+ $plugin_meta['ajdg-gateway-help'] = sprintf( '<a href="%s" target="_blank">%s</a>', 'https://ajdg.solutions/forums/forum/woocommerce-plugins/?pk_campaign=woo_gateway_stats', 'Support' );
+ }
- return $links;
+ return $plugin_meta;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment