Skip to content

Commit

Permalink
Merge pull request #74 from WebDevStudios/feature/CC-73-simplify-cart…
Browse files Browse the repository at this point in the history
…_hash

CC-73: rework cart_hash to be simple md5 string
  • Loading branch information
ggwicz authored Nov 14, 2019
2 parents 01d887d + f3fc1e9 commit 06205ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/AbandonedCarts/CartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static function get_cart_data( $select, $where, $where_values ) {
*/
public static function get_cart_hash( int $cart_id ) {
return self::get_cart_data(
'HEX(cart_hash)',
'cart_hash',
'cart_id = %d',
[
intval( $cart_id ),
Expand All @@ -196,7 +196,7 @@ public static function get_cart_hash( int $cart_id ) {
public static function get_cart_contents( $cart_hash ) {
return self::get_cart_data(
'cart_contents',
'cart_hash = UNHEX(%s)',
'cart_hash = %s',
[
$cart_hash,
]
Expand Down Expand Up @@ -237,7 +237,7 @@ protected function save_cart_data( $user_id, $customer_data ) {
%d,
%s,
%d,
UNHEX(MD5(CONCAT(user_id, user_email)))
MD5(CONCAT(user_id, user_email))
) ON DUPLICATE KEY UPDATE `cart_updated` = VALUES(`cart_updated`), `cart_updated_ts` = VALUES(`cart_updated_ts`), `cart_contents` = VALUES(`cart_contents`)",
$user_id,
$customer_data['billing']['email'],
Expand Down
12 changes: 10 additions & 2 deletions src/AbandonedCarts/CartsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CartsTable extends Service {
*
* @since 2019-10-09
*/
const DB_VERSION = '1.3';
const DB_VERSION = '1.4';

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

// Update `cart_hash` field for all records in versions older than 1.4.
if ( floatval( self::DB_VERSION_OPTION_NAME ) < 1.4 ) {
$wpdb->query(
"UPDATE {$table_name}
SET cart_hash = HEX(cart_hash)"
);
}

// Any data updates would be performed here.
update_option( self::DB_VERSION_OPTION_NAME, self::DB_VERSION );
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rest/AbandonedCarts/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private function get_cart_data( int $per_page, int $offset, string $date_min, st
cart_updated_ts,
cart_created,
cart_created_ts,
HEX(cart_hash) as cart_hash
cart_hash
FROM {$table_name}
{$dates_where}
ORDER BY cart_updated_ts
Expand Down

0 comments on commit 06205ae

Please sign in to comment.