Last active
March 29, 2023 11:28
-
-
Save nhadsall/89a71f6c7ca54dcb13063e15977628a2 to your computer and use it in GitHub Desktop.
Fix WooCommerce get_total_spent()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SPROUT_order_total { | |
private $orders = []; | |
function __construct() { | |
add_action( "woocommerce_before_order_object_save", [ $this, 'save_order' ], 1000, 2 ); | |
add_filter( "woocommerce_customer_get_total_spent_query", [ $this, 'intercept_query' ], 1000, 2 ); | |
} | |
function intercept_query( $query, $customer ) { | |
global $wpdb; | |
$user_id = $customer->get_id(); | |
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() ); | |
$sql = "SELECT SUM(order_total) FROM wp_sprout_order_totals | |
WHERE user_id = %d | |
AND order_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )"; | |
$sql = $wpdb->prepare( $sql, $user_id ); | |
error_log( 'NEW: ' . $sql ); | |
return $sql; | |
} | |
function save_order( $instance, $data_store ) { | |
$order_id = $instance->get_id(); | |
$user_id = $instance->get_user_id(); | |
$order_total = floatval( $instance->get_total() ); | |
$order_status = 'wc-' . $instance->get_status(); | |
$this->write_data( $order_id, $user_id, $order_total, $order_status ); | |
} | |
function write_data( $order_id, $user_id, $order_total, $order_status ) { | |
global $wpdb; | |
$sql = " | |
INSERT INTO {$wpdb->prefix}sprout_order_totals (order_id,user_id,order_total,order_status) | |
VALUES (%d,%d,%f,%s) | |
ON DUPLICATE KEY UPDATE | |
order_id = VALUES(order_id), | |
user_id = VALUES(user_id), | |
order_total = VALUES(order_total), | |
order_status = VALUES(order_status)"; | |
$sql = $wpdb->prepare( $sql, $order_id, $user_id, $order_total, $order_status ); | |
$wpdb->query( $sql ); | |
} | |
} | |
new SPROUT_order_total; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################### | |
# Create Table | |
#################################### | |
CREATE TABLE `wp_sprout_order_totals` ( | |
`order_id` int(11) NOT NULL, | |
`user_id` int(11) DEFAULT NULL, | |
`order_total` decimal(8,2) DEFAULT 0.00, | |
`order_status` varchar(255) NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; | |
ALTER TABLE `wp_sprout_order_totals` | |
ADD PRIMARY KEY (`order_id`), | |
ADD KEY `user_id` (`user_id`), | |
ADD KEY `order_status` (`order_status`); | |
#################################### | |
# Import Historical Data | |
#################################### | |
INSERT INTO wp_sprout_order_totals ( order_id, user_id, order_total, order_status) | |
SELECT | |
ID, | |
meta.meta_value AS user_id, | |
CASE when meta2.meta_value IS NULL or meta2.meta_value = '' | |
THEN '0' | |
ELSE meta2.meta_value | |
END as order_total, | |
posts.post_status AS order_status | |
FROM wp_posts as posts | |
LEFT JOIN wp_postmeta as meta | |
ON posts.ID = meta.post_id | |
LEFT JOIN wp_postmeta AS meta2 | |
ON posts.ID = meta2.post_id | |
WHERE meta.meta_key = '_customer_user' | |
AND posts.post_type = 'shop_order' | |
AND meta2.meta_key = '_order_total' | |
ON DUPLICATE KEY UPDATE | |
order_id = VALUES(order_id), | |
user_id = VALUES(user_id), | |
order_total = VALUES(order_total), | |
order_status = VALUES(order_status); | |
#################################### | |
# Test Query | |
#################################### | |
SELECT SUM(order_total) FROM wp_sprout_order_totals | |
WHERE user_id = 38676 | |
AND order_status IN ( 'wc-completed', 'wc-processing'); | |
#################################### | |
# horrible original query | |
#################################### | |
SELECT SUM(meta2.meta_value) | |
FROM wp_posts as posts | |
LEFT JOIN wp_postmeta AS meta ON posts.ID = meta.post_id | |
LEFT JOIN wp_postmeta AS meta2 ON posts.ID = meta2.post_id | |
WHERE meta.meta_key = '_customer_user' | |
AND meta.meta_value = 38676 | |
AND posts.post_type = 'shop_order' | |
AND posts.post_status IN ( 'wc-completed', 'wc-processing' ) | |
AND meta2.meta_key = '_order_total'; |
Sorry, I didn't realize I left this public and I'm not able to provide support on it. It was designed to be used manually by myself to make woo perform better. It really wasn't intended for general use. If you don't know what every line here does, I don't recommend using it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, the php file i can put into a folder in wp-content/plugins to makeit work ?