PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 2.7.0
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v2.7.0
2.7.0 2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 All 43 releases
sellkit / includes / contact-segmentation / rfm / user-updater.php

user-updater.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 2.7.0, at includes/contact-segmentation/rfm/user-updater.php

110 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Contact_Segmentation\rfm;
4
5 use Sellkit\Database;
6
7 defined( 'ABSPATH' ) || die();
8
9 /**
10 * Class RFM user data updater.
11 *
12 * @package Sellkit\Contact_Segmentation
13 * @SuppressWarnings(ExcessiveClassComplexity)
14 * @since 1.1.0
15 */
16 class User_Updater extends \WP_Background_Process {
17
18 /**
19 * Queue Action.
20 *
21 * @since 1.1.0
22 * @var string
23 */
24 protected $action = 'rfm_user_updating';
25
26 /**
27 * Override this method to perform any actions required on each
28 * queue item. Return the modified item for further processing
29 * in the next pass through. Or, return false to remove the
30 * item from the queue.
31 *
32 * @param mixed $email Queue item to iterate over.
33 *
34 * @return mixed
35 */
36 protected function task( $email ) {
37 $this->update_users( $email );
38
39 return false;
40 }
41
42 /**
43 * Updates the users.
44 *
45 * @since 1.1.0
46 * @param array $email Email.
47 */
48 public function update_users( $email ) {
49 global $wpdb;
50
51 $sellkit_prefix = Database::DATABASE_PREFIX;
52
53 // phpcs:disable
54 $contact_segmentation_user = $wpdb->get_results(
55 $wpdb->prepare( "SELECT * from {$wpdb->prefix}{$sellkit_prefix}contact_segmentation
56 where email = %s", $email )
57 );
58 // phpcs:enable
59
60 if ( ! empty( $contact_segmentation_user ) ) {
61 return;
62 }
63
64 $current_user = get_user_by( 'email', $email );
65
66 $args = [
67 'customer_id' => $current_user->ID,
68 'post_status' => 'completed',
69 'post_type' => 'shop_order',
70 'limit' => -1,
71 'orderby' => 'id',
72 'order' => 'DESC',
73 ];
74
75 $orders = function_exists( 'WC' ) ? wc_get_orders( $args ) : [];
76 $order_number = count( $orders );
77 $last_order = ! empty( $orders[0] ) ? $orders[0] : '';
78 $total_spent = 0;
79
80 foreach ( $orders as $order ) {
81 $total_spent += $order->get_total();
82 }
83
84 $last_order_date = $last_order ? strtotime( $last_order->get_date_completed()->date( 'Y-m-d H:i:s' ) ) : '';
85
86 sellkit()->db->insert( 'contact_segmentation', [
87 'email' => $email,
88 'last_order_date' => $last_order_date,
89 'total_spent' => $total_spent,
90 'total_order_count' => $order_number,
91 ] );
92 }
93
94 /**
95 * Complete
96 *
97 * Override if applicable, but ensure that the below actions are
98 * performed, or, call parent::complete().
99 *
100 * @since 1.1.0
101 */
102 protected function complete() {
103 parent::complete();
104
105 sellkit_update_option( 'contact_segmentation_users_are_imported', true );
106 sellkit_update_option( 'contact_segmentation_users_updating_started', false );
107 do_action( 'sellkit_update_rfm_score' );
108 }
109 }
110