WooCommerce Code Reference

WC_Customer_Data_Store extends WC_Data_Store_WP
in package
implements WC_Customer_Data_Store_Interface, WC_Object_Data_Store_Interface

WC Customer Data Store.

Tags
version
3.0.0

Interfaces, Classes and Traits

WC_Customer_Data_Store_Interface
Customer Data Store Interface
WC_Object_Data_Store_Interface
Object Data Store Interface

Table of Contents

$internal_meta_keys  : array
Data stored in meta keys, but not considered "meta".
$meta_type  : string
Internal meta type used to store user data.
$must_exist_meta_keys  : array
Meta data which should exist in the DB, even if empty.
$object_id_field_for_meta  : string
This only needs set if you are using a custom metadata type (for example payment tokens.
add_meta()  : int
Add new piece of meta.
create()  : mixed
Method to create a new customer in the database.
delete()  : bool
Deletes a customer from the database.
delete_from_lookup_table()  : mixed
Delete lookup table data for an ID.
delete_meta()  : mixed
Deletes meta based on meta ID.
get_internal_meta_keys()  : array
Return list of internal meta keys.
get_last_order()  : WC_Order|false
Gets the customers last order.
get_order_count()  : int
Return the number of orders this customer has.
get_total_spent()  : float
Return how much money this customer has spent.
get_user_ids_for_billing_email()  : array
Get all user ids who have `billing_email` set to any of the email passed in array.
parse_date_for_wp_query()  : array
Map a valid date query var to WP_Query arguments.
read()  : mixed
Method to read a customer object.
read_meta()  : array
Returns an array of meta for an object.
search_customers()  : array
Search customers and return customer IDs.
update()  : mixed
Updates a customer in the database.
update_meta()  : mixed
Update meta.
exclude_internal_meta_keys()  : bool
Callback to remove unwanted meta data.
get_data_for_lookup_table()  : array
Get data to save to a lookup table.
get_db_info()  : array
Table structure is slightly different between meta types, this function will return what we need to know.
get_primary_key_for_lookup_table()  : string
Get primary key name for lookup table.
get_props_to_update()  : array
Gets a list of props and meta keys that need updated based on change state or if they are present in the database or not.
get_search_stopwords()  : array
Retrieve stopwords used when parsing search terms.
get_term_ids()  : array
Get and store terms from a taxonomy.
get_valid_search_terms()  : array
Check if the terms are suitable for searching.
get_wp_query_args()  : array
Get valid WP_Query args from a WC_Object_Query's query variables.
prefix_key()  : string
Internal meta keys we don't want exposed as part of meta_data. This is in addition to all data props with _ prefix.
update_lookup_table()  : null
Update a lookup table for an object.
update_or_delete_post_meta()  : bool
Update meta data in, or delete it from, the database.
update_user_meta()  : mixed
Helper method that updates all the meta for a customer. Used for update & create.

Properties

$internal_meta_keys

Data stored in meta keys, but not considered "meta".

protected array $internal_meta_keys = array('locale', 'billing_postcode', 'billing_city', 'billing_address_1', 'billing_address_2', 'billing_state', 'billing_country', 'shipping_postcode', 'shipping_city', 'shipping_address_1', 'shipping_address_2', 'shipping_state', 'shipping_country', 'paying_customer', 'last_update', 'first_name', 'last_name', 'display_name', 'show_admin_bar_front', 'use_ssl', 'admin_color', 'rich_editing', 'comment_shortcuts', 'dismissed_wp_pointers', 'show_welcome_panel', 'session_tokens', 'nickname', 'description', 'billing_first_name', 'billing_last_name', 'billing_company', 'billing_phone', 'billing_email', 'shipping_first_name', 'shipping_last_name', 'shipping_company', 'wptests_capabilities', 'wptests_user_level', 'syntax_highlighting', '_order_count', '_money_spent', '_woocommerce_tracks_anon_id')
Tags
since
3.0.0

$object_id_field_for_meta

This only needs set if you are using a custom metadata type (for example payment tokens.

protected string $object_id_field_for_meta = ''

This should be the name of the field your table uses for associating meta with objects. For example, in payment_tokenmeta, this would be payment_token_id.

Methods

delete_from_lookup_table()

Delete lookup table data for an ID.

public delete_from_lookup_table(int $id, string $table) : mixed
Parameters
$id : int

ID of object to update.

$table : string

Lookup table name.

Tags
since
3.6.0
Return values
mixed

get_user_ids_for_billing_email()

Get all user ids who have `billing_email` set to any of the email passed in array.

public get_user_ids_for_billing_email(array $emails) : array
Parameters
$emails : array

List of emails to check against.

Return values
array

parse_date_for_wp_query()

Map a valid date query var to WP_Query arguments.

public parse_date_for_wp_query(mixed $query_var, string $key[, array $wp_query_args = array() ]) : array

Valid date formats: YYYY-MM-DD or timestamp, possibly combined with an operator from $valid_operators. Also accepts a WC_DateTime object.

Parameters
$query_var : mixed

A valid date format.

$key : string

meta or db column key.

$wp_query_args : array = array()

WP_Query args.

Tags
since
3.2.0
Return values
arrayModified $wp_query_args

search_customers()

Search customers and return customer IDs.

public search_customers(string $term[, int|string $limit = '' ]) : array
Parameters
$term : string

Search term.

$limit : int|string = ''

Limit search results.

Tags
since
3.0.7
Return values
array

get_data_for_lookup_table()

Get data to save to a lookup table.

protected get_data_for_lookup_table(int $id, string $table) : array
Parameters
$id : int

ID of object to update.

$table : string

Lookup table name.

Tags
since
3.6.0
Return values
array

get_db_info()

Table structure is slightly different between meta types, this function will return what we need to know.

protected get_db_info() : array
Tags
since
3.0.0
Return values
arrayArray elements: table, object_id_field, meta_id_field

get_primary_key_for_lookup_table()

Get primary key name for lookup table.

protected get_primary_key_for_lookup_table(string $table) : string
Parameters
$table : string

Lookup table name.

Tags
since
3.6.0
Return values
string

get_props_to_update()

Gets a list of props and meta keys that need updated based on change state or if they are present in the database or not.

protected get_props_to_update(WC_Data $object, array $meta_key_to_props[, string $meta_type = 'post' ]) : array
Parameters
$object : WC_Data

The WP_Data object (WC_Coupon for coupons, etc).

$meta_key_to_props : array

A mapping of meta keys => prop names.

$meta_type : string = 'post'

The internal WP meta type (post, user, etc).

Return values
arrayA mapping of meta keys => prop names, filtered by ones that should be updated.

get_term_ids()

Get and store terms from a taxonomy.

protected get_term_ids(WC_Data|int $object, string $taxonomy) : array
Parameters
$object : WC_Data|int

WC_Data object or object ID.

$taxonomy : string

Taxonomy name e.g. product_cat.

Tags
since
3.0.0
Return values
arrayof terms

get_valid_search_terms()

Check if the terms are suitable for searching.

protected get_valid_search_terms(array $terms) : array

Uses an array of stopwords (terms) that are excluded from the separate term matching when searching for posts. The list of English stopwords is the approximate search engines list, and is translatable.

Parameters
$terms : array

Terms to check.

Tags
since
3.4.0
Return values
arrayTerms that are not stopwords.

get_wp_query_args()

Get valid WP_Query args from a WC_Object_Query's query variables.

protected get_wp_query_args(array $query_vars) : array
Parameters
$query_vars : array

query vars from a WC_Object_Query.

Tags
since
3.1.0
Return values
array

prefix_key()

Internal meta keys we don't want exposed as part of meta_data. This is in addition to all data props with _ prefix.

protected prefix_key(string $key) : string
Parameters
$key : string

Prefix to be added to meta keys.

Tags
since
2.6.0
Return values
string

update_lookup_table()

Update a lookup table for an object.

protected update_lookup_table(int $id, string $table) : null
Parameters
$id : int

ID of object to update.

$table : string

Lookup table name.

Tags
since
3.6.0
Return values
null

update_or_delete_post_meta()

Update meta data in, or delete it from, the database.

protected update_or_delete_post_meta(WC_Data $object, string $meta_key, mixed $meta_value) : bool

Avoids storing meta when it's either an empty string or empty array. Other empty values such as numeric 0 and null should still be stored. Data-stores can force meta to exist using must_exist_meta_keys.

Note: WordPress get_metadata function returns an empty string when meta data does not exist.

Parameters
$object : WC_Data

The WP_Data object (WC_Coupon for coupons, etc).

$meta_key : string

Meta key to update.

$meta_value : mixed

Value to save.

Tags
since
3.6.0

Added to prevent empty meta being stored unless required.

Return values
boolTrue if updated/deleted.