session->get( 'shpn_returning_customer' ) ) { return; } $cart_id = WC()->cart->get_cart_hash(); if ( null === WC()->session->get( 'shpn_cart_id' ) ) { WC()->session->set( 'shpn_cart_id', $cart_id ); } else { $cart_id = WC()->session->get( 'shpn_cart_id' ); } $current_time = current_time( 'timestamp' ); // @codingStandardsIgnoreLine we are using this for better time comparision. $cutoff_time = get_option( 'shopnotify_cart_timeout', 10 ); $cutoff_time = $cutoff_time * 60; $cart_cutoff_time = $current_time - $cutoff_time; $table = $wpdb->prefix . 'shpn_abandoned_cart'; $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE user_id = %d AND cart_abandoned = %d", $user_id, 0 ) ); $cart_data = WC()->cart->get_cart_contents(); $session_id = WC()->session->get_customer_id(); if ( null === $result || 0 === count( $result ) ) { // add new cart. $wpdb->insert( $table, array( 'user_id' => $user_id, 'cart_id' => $cart_id, 'cart_data' => wp_json_encode( $cart_data ), 'timestamp' => $current_time, 'session_id' => $session_id, 'order_id' => null, ), array( '%d', '%s', '%s', '%d', '%s', '%d' ) ); } elseif ( isset( $result[0]->timestamp ) && $result[0]->timestamp > $cart_cutoff_time && $result[0]->cart_id === $cart_id ) { // same cart update. $wpdb->update( $table, array( 'cart_data' => wp_json_encode( $cart_data ), 'timestamp' => $current_time, ), array( 'id' => $result[0]->id ), array( '%s', '%d' ), array( '%d' ) ); } else { // set old cart abandoned. $wpdb->update( $table, array( 'cart_abandoned' => 1, 'timestamp' => $current_time, ), array( 'id' => $result[0]->id ), array( '%d', '%d' ), array( '%d' ) ); // add new cart. $wpdb->insert( $table, array( 'user_id' => $user_id, 'cart_id' => $cart_id, 'cart_data' => wp_json_encode( $cart_data ), 'timestamp' => $current_time, 'session_id' => $session_id, 'order_id' => null, ), array( '%d', '%s', '%s', '%d', '%s', '%d' ) ); } } }