| 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 |
|