PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.6.2
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.6.2
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 1.8.1 All 42 releases
sellkit / includes / contact-segmentation / rfm / updater.php

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

112 lines 2.4 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 Data Updater.
11 *
12 * @package Sellkit\Contact_Segmentation
13 * @SuppressWarnings(ExcessiveClassComplexity)
14 * @since 1.1.0
15 */
16 class Updater extends \WP_Background_Process {
17
18 /**
19 * Queue Action.
20 *
21 * @since 1.1.0
22 * @var string
23 */
24 protected $action = 'rfm_updating_process';
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 $item Queue item to iterate over.
33 *
34 * @return mixed
35 */
36 protected function task( $item ) {
37 $this->update_rfm( $item );
38
39 return false;
40 }
41
42 /**
43 * Calculate RFM data and update it.
44 *
45 * @since 1.1.0
46 * @param array $data RFM data.
47 */
48 public function update_rfm( $data ) {
49 global $wpdb;
50
51 $sellkit_prefix = Database::DATABASE_PREFIX;
52
53 $limit = $data['max_number'];
54 $offset = $data['max_number'] - 100;
55
56 // phpcs:disable
57 $contacts = $wpdb->get_results(
58 $wpdb->prepare( "SELECT
59 id, last_order_date, total_spent, total_order_count
60 from {$wpdb->prefix}{$sellkit_prefix}contact_segmentation
61 order by id asc limit %d offset %d", $limit, $offset)
62 );
63 // phpcs:enable
64
65 if ( is_wp_error( $contacts ) ) {
66 new \WP_Error( __( 'Somethings went wrong', 'sellkit' ) );
67 }
68
69 if ( empty( $contacts ) ) {
70 return;
71 }
72
73 foreach ( $contacts as $contact ) {
74 $rfm_data = [
75 'rfm_r' => self::get_score( intval( $data['max_recency'] ) - intval( $data['min_recency'] ), intval( $contact->last_order_date ) - intval( $data['min_recency'] ) ),
76 'rfm_f' => self::get_score( $data['max_frequency'], $contact->total_order_count ),
77 'rfm_m' => self::get_score( $data['max_monetary'], $contact->total_spent ),
78 ];
79
80 sellkit()->db->update( 'contact_segmentation', $rfm_data, [ 'id' => $contact->id ] );
81 }
82 }
83
84 /**
85 * Gets RFM score.
86 *
87 * @param int $max_value Max value.
88 * @param int $current_value Current value.
89 * @return int
90 * @since 1.1.0
91 */
92 public static function get_score( $max_value, $current_value ) {
93 if ( $current_value <= $max_value * 0.2 ) {
94 return 1;
95 }
96
97 if ( $current_value <= $max_value * 0.4 ) {
98 return 2;
99 }
100
101 if ( $current_value <= $max_value * 0.6 ) {
102 return 3;
103 }
104
105 if ( $current_value <= $max_value * 0.8 ) {
106 return 4;
107 }
108
109 return 5;
110 }
111 }
112