Skip to content

Commit a759d26

Browse files
committed
code review feedback fixes
1 parent c8908e4 commit a759d26

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/AbandonedCarts/CartHandler.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use WebDevStudios\OopsWP\Structure\Service;
1313
use WC_Customer;
14+
use WC_Order;
1415
use DateTime;
1516
use DateInterval;
1617

@@ -30,15 +31,15 @@ class CartHandler extends Service {
3031
* @since 2019-10-11
3132
*/
3233
public function register_hooks() {
33-
add_action( 'woocommerce_after_template_part', [ $this, 'check_template' ], 10, 4 );
34+
add_action( 'woocommerce_after_template_part', [ $this, 'save_or_clear_cart_data' ], 10, 4 );
3435
add_action( 'woocommerce_checkout_process', [ $this, 'update_cart_data' ] );
35-
add_action( 'check_expired_carts', [ $this, 'check_expired_carts' ] );
36+
add_action( 'cc_woo_check_expired_carts', [ $this, 'delete_expired_carts' ] );
3637
add_action( 'woocommerce_calculate_totals', [ $this, 'update_cart_data' ] );
3738
add_action( 'woocommerce_cart_item_removed', [ $this, 'update_cart_data' ] );
3839
}
3940

4041
/**
41-
* Check current WC template.
42+
* Either call an update of cart data which will be saved or remove cart data based on what template we arrive at.
4243
*
4344
* @author Rebekah Van Epps <[email protected]>
4445
* @since 2019-10-11
@@ -47,13 +48,14 @@ public function register_hooks() {
4748
* @param string $located Full local path to current template file.
4849
* @param array $args Template args.
4950
*/
50-
public function check_template( $template_name, $template_path, $located, $args ) {
51+
public function save_or_clear_cart_data( $template_name, $template_path, $located, $args ) {
5152
// If checkout page displayed, save cart data.
5253
if ( 'checkout/form-checkout.php' === $template_name ) {
5354
$this->update_cart_data();
5455
}
56+
5557
// If thankyou page displayed, clear cart data.
56-
if ( 'checkout/thankyou.php' === $template_name ) {
58+
if ( isset( $args['order'] ) && 'checkout/thankyou.php' === $template_name ) {
5759
$this->clear_purchased_data( $args['order'] );
5860
}
5961
}
@@ -81,6 +83,8 @@ public function update_cart_data() {
8183
$customer_data['billing'] = array_merge( $customer_data['billing'], WC()->customer->get_billing() );
8284
$customer_data['shipping'] = array_merge( $customer_data['shipping'], WC()->customer->get_shipping() );
8385

86+
write_log( $_POST, 'posted' );
87+
8488
// Check if submission attempted.
8589
if ( isset( $_POST['woocommerce_checkout_place_order'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- Okay use of $_POST data.
8690
// Update customer data from posted data.
@@ -263,7 +267,7 @@ protected function save_cart_data( $user_id, $customer_data ) {
263267
* @return void
264268
*/
265269
public function clear_purchased_data( $order ) {
266-
if ( false === $order ) {
270+
if ( empty( $order ) ) {
267271
return;
268272
}
269273

@@ -301,12 +305,13 @@ protected function remove_cart_data( $user_id, $user_email ) {
301305
* @author Rebekah Van Epps <[email protected]>
302306
* @since 2019-10-11
303307
*/
304-
public function check_expired_carts() {
308+
public function delete_expired_carts() {
305309
global $wpdb;
306310

307311
// Delete all carts at least 30 days old.
308312
$table_name = CartsTable::get_table_name();
309-
$test = $wpdb->query(
313+
314+
$wpdb->query(
310315
$wpdb->prepare(
311316
// phpcs:disable WordPress.DB.PreparedSQL -- Okay use of unprepared variable for table name in SQL.
312317
"DELETE FROM {$table_name}

0 commit comments

Comments
 (0)