Skip to content

Commit 06205ae

Browse files
authored
Merge pull request #74 from WebDevStudios/feature/CC-73-simplify-cart_hash
CC-73: rework cart_hash to be simple md5 string
2 parents 01d887d + f3fc1e9 commit 06205ae

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/AbandonedCarts/CartHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public static function get_cart_data( $select, $where, $where_values ) {
177177
*/
178178
public static function get_cart_hash( int $cart_id ) {
179179
return self::get_cart_data(
180-
'HEX(cart_hash)',
180+
'cart_hash',
181181
'cart_id = %d',
182182
[
183183
intval( $cart_id ),
@@ -196,7 +196,7 @@ public static function get_cart_hash( int $cart_id ) {
196196
public static function get_cart_contents( $cart_hash ) {
197197
return self::get_cart_data(
198198
'cart_contents',
199-
'cart_hash = UNHEX(%s)',
199+
'cart_hash = %s',
200200
[
201201
$cart_hash,
202202
]
@@ -237,7 +237,7 @@ protected function save_cart_data( $user_id, $customer_data ) {
237237
%d,
238238
%s,
239239
%d,
240-
UNHEX(MD5(CONCAT(user_id, user_email)))
240+
MD5(CONCAT(user_id, user_email))
241241
) ON DUPLICATE KEY UPDATE `cart_updated` = VALUES(`cart_updated`), `cart_updated_ts` = VALUES(`cart_updated_ts`), `cart_contents` = VALUES(`cart_contents`)",
242242
$user_id,
243243
$customer_data['billing']['email'],

src/AbandonedCarts/CartsTable.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CartsTable extends Service {
2525
*
2626
* @since 2019-10-09
2727
*/
28-
const DB_VERSION = '1.3';
28+
const DB_VERSION = '1.4';
2929

3030
/**
3131
* Option name for abandoned carts db version.
@@ -71,7 +71,7 @@ public function create_table() {
7171
cart_updated_ts int(11) unsigned NOT NULL DEFAULT 0,
7272
cart_created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
7373
cart_created_ts int(11) unsigned NOT NULL DEFAULT 0,
74-
cart_hash binary(16) NOT NULL DEFAULT 0,
74+
cart_hash char(32) NOT NULL DEFAULT '',
7575
PRIMARY KEY (cart_id),
7676
UNIQUE KEY cart_hash (cart_hash)
7777
) {$wpdb->get_charset_collate()}";
@@ -96,6 +96,14 @@ protected function update_table() {
9696
// phpcs:disable WordPress.DB.PreparedSQL -- Okay use of unprepared variable for table name in SQL.
9797
if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) ) {
9898

99+
// Update `cart_hash` field for all records in versions older than 1.4.
100+
if ( floatval( self::DB_VERSION_OPTION_NAME ) < 1.4 ) {
101+
$wpdb->query(
102+
"UPDATE {$table_name}
103+
SET cart_hash = HEX(cart_hash)"
104+
);
105+
}
106+
99107
// Any data updates would be performed here.
100108
update_option( self::DB_VERSION_OPTION_NAME, self::DB_VERSION );
101109
}

src/Rest/AbandonedCarts/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private function get_cart_data( int $per_page, int $offset, string $date_min, st
195195
cart_updated_ts,
196196
cart_created,
197197
cart_created_ts,
198-
HEX(cart_hash) as cart_hash
198+
cart_hash
199199
FROM {$table_name}
200200
{$dates_where}
201201
ORDER BY cart_updated_ts

0 commit comments

Comments
 (0)