Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
Only add the filters for WC_Customer_Data_Store_Custom_Table once
Browse files Browse the repository at this point in the history
Instead of adding the hooks in the constructor (potentially causing a *ton* of hooked callbacks), register the customer data store hooks once.
  • Loading branch information
stevegrunwell committed Mar 3, 2020
1 parent 8773c73 commit 7bbd2ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 11 additions & 11 deletions includes/class-wc-customer-data-store-custom-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@
*/
class WC_Customer_Data_Store_Custom_Table extends WC_Customer_Data_Store {

/**
* Register callbacks when the data store is first instantiated.
*/
public function __construct() {
add_filter( 'woocommerce_pre_customer_bought_product', __CLASS__ . '::pre_customer_bought_product', 10, 4 );

// Handle deleting a customer from the database.
add_action( 'deleted_user', __CLASS__ . '::reset_order_customer_id_on_deleted_user' );
remove_action( 'deleted_user', 'wc_reset_order_customer_id_on_deleted_user' );
}

/**
* Gets the customers last order.
*
Expand Down Expand Up @@ -139,6 +128,17 @@ public function get_total_spent( &$customer ) {
return $spent;
}

/**
* Register callbacks when the data store is first instantiated.
*/
public static function add_hooks() {
add_filter( 'woocommerce_pre_customer_bought_product', __CLASS__ . '::pre_customer_bought_product', 10, 4 );

// Handle deleting a customer from the database.
add_action( 'deleted_user', __CLASS__ . '::reset_order_customer_id_on_deleted_user' );
remove_action( 'deleted_user', 'wc_reset_order_customer_id_on_deleted_user' );
}

/**
* Short-circuit the wc_customer_bought_product() function.
*
Expand Down
2 changes: 2 additions & 0 deletions includes/class-woocommerce-custom-orders-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function setup() {
// When associating previous orders with a customer based on email, update the record.
add_action( 'woocommerce_update_new_customer_past_order', 'WooCommerce_Custom_Orders_Table_Filters::update_past_customer_order', 10, 2 );

WC_Customer_Data_Store_Custom_Table::add_hooks();

// Register the table within WooCommerce.
add_filter( 'woocommerce_install_get_tables', array( $this, 'register_table_name' ) );

Expand Down

0 comments on commit 7bbd2ba

Please sign in to comment.