WooCommerce Code Reference

Search results

WC_Product_Grouped_Data_Store_CPT extends WC_Product_Data_Store_CPT implements WC_Object_Data_Store_Interface

WC Grouped Product Data Store: Stored in CPT.

Table of Contents

$internal_meta_keys Data stored in meta keys, but not considered "meta" for an object. array
$must_exist_meta_keys Meta data which should exist in the DB, even if empty. array
$extra_data_saved If we have already saved our extra data, don't do automatic / default handling. bool
$updated_props Stores updated props. array
$meta_type Meta type. This should match up with the types available at https://developer.wordpress.org/reference/functions/add_metadata/. string
$object_id_field_for_meta This only needs set if you are using a custom metadata type (for example payment tokens. string
update_post_meta() Helper method that updates all the post meta for a grouped product. mixed
handle_updated_props() Handle updated meta props after updating meta data. mixed
sync_price() Sync grouped product prices with children. mixed
update_prices_from_children() Loop over child products and update the grouped product prices. mixed
create() Method to create a new product in the database. mixed
read() Method to read a product from the database. mixed
update() Method to update a product in the database. mixed
delete() Method to delete a product from the database. bool
read_product_data() Read product data. Can be overridden by child classes to load other props. mixed
read_stock_quantity() Re-reads stock from the DB ignoring changes. mixed
read_extra_data() Read extra data associated with the product, like button text or product URL for external products. mixed
read_visibility() Convert visibility terms to props. mixed
read_attributes() Read attributes from post meta. mixed
read_downloads() Read downloads from post meta. mixed
update_post_meta() Helper method that updates all the post meta for a product based on it's settings in the WC_Product class. mixed
handle_updated_props() Handle updated meta props after updating meta data. mixed
update_terms() For all stored terms in all taxonomies, save them to the DB. mixed
update_visibility() Update visibility terms based on props. mixed
update_attributes() Update attributes which are a mix of terms and meta data. mixed
update_downloads() Update downloads. bool
update_version_and_type() Make sure we store the product type and version (to track data changes). mixed
clear_caches() Clear any caches. mixed
get_on_sale_products() Returns an array of on sale products, as an array of objects with an ID and parent_id present. Example: $return[0]->id, $return[0]->parent_id. array
get_featured_product_ids() Returns a list of product IDs ( id as key => parent as value) that are featured. Uses get_posts instead of wc_get_products since we want some extra meta queries and ALL products (posts_per_page = -1). array
is_existing_sku() Check if product sku is found for any other product IDs. bool
get_product_id_by_sku() Return product ID based on SKU. int
get_starting_sales() Returns an array of IDs of products that have sales starting soon. array
get_ending_sales() Returns an array of IDs of products that have sales which are due to end. array
find_matching_product_variation() Find a matching (enabled) variation within a variable product. int
create_all_product_variations() Creates all possible combinations of variations from the attributes, without creating duplicates. int
sort_all_product_variations() Make sure all variations have a sort order set so they can be reordered correctly. mixed
get_related_products() Return a list of related products (using data like categories and IDs). array
get_related_products_query() Builds the related posts query. array
set_product_stock() Update a product's stock amount directly in the database. mixed
update_product_stock() Update a product's stock amount directly. int|float
update_product_sales() Update a product's sale count directly. mixed
update_average_rating() Update a products average rating meta. mixed
update_review_count() Update a products review count meta. mixed
update_rating_counts() Update a products rating counts. mixed
get_shipping_class_id_by_slug() Get shipping class ID by slug. int|false
get_products() Returns an array of products. array|object
search_products() Search product data for a term and return ids. array
get_product_type() Get the product type based on product ID. bool|string
reviews_allowed_query_where() Add ability to get products by 'reviews_allowed' in WC_Product_Query. string
get_wp_query_args() Get valid WP_Query args from a WC_Object_Query's query variables. array
query() Query for Products matching specific criteria. array|object
get_data_for_lookup_table() Get data to save to a lookup table. array
get_primary_key_for_lookup_table() Get primary key name for lookup table. string
get_term_ids() Get and store terms from a taxonomy. array
read_meta() Returns an array of meta for an object. array
delete_meta() Deletes meta based on meta ID. mixed
add_meta() Add new piece of meta. int
update_meta() Update meta. mixed
get_db_info() Table structure is slightly different between meta types, this function will return what we need to know. 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. string
exclude_internal_meta_keys() Callback to remove unwanted meta data. bool
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. array
update_or_delete_post_meta() Update meta data in, or delete it from, the database. bool
parse_date_for_wp_query() Map a valid date query var to WP_Query arguments. array
get_internal_meta_keys() Return list of internal meta keys. array
get_valid_search_terms() Check if the terms are suitable for searching. array
get_search_stopwords() Retrieve stopwords used when parsing search terms. array
update_lookup_table() Update a lookup table for an object. null
delete_from_lookup_table() Delete lookup table data for an ID. mixed

Properties

$internal_meta_keys

Data stored in meta keys, but not considered "meta" for an object.

protected array $internal_meta_keys = array()

$must_exist_meta_keys

Meta data which should exist in the DB, even if empty.

protected array $must_exist_meta_keys = array()

$extra_data_saved

If we have already saved our extra data, don't do automatic / default handling.

protected bool $extra_data_saved = \false

$meta_type

Meta type. This should match up with the types available at https://developer.wordpress.org/reference/functions/add_metadata/.

protected string $meta_type = 'post'
WP defines 'post', 'user', 'comment', and 'term'.

$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

update_post_meta()

Helper method that updates all the post meta for a grouped product.

protected update_post_meta( &$product : WC_Product [, $force : bool = false ] ) : mixed
Parameters
$product : WC_Product

Product object.

$force : bool = false

Force update. Used during create.

Tags
since
Return values
mixed

delete()

Method to delete a product from the database.

public delete( &$product : WC_Product [, $args : array = array() ] ) : bool
Parameters
$product : WC_Product

Product object.

$args : array = array()

Array of args to pass to the delete method.

Return values
bool

result

read_product_data()

Read product data. Can be overridden by child classes to load other props.

protected read_product_data( &$product : WC_Product ) : mixed
Parameters
$product : WC_Product

Product object.

Tags
since
Return values
mixed

read_stock_quantity()

Re-reads stock from the DB ignoring changes.

public read_stock_quantity( &$product : WC_Product [, $new_stock : int|float = null ] ) : mixed
Parameters
$product : WC_Product

Product object.

$new_stock : int|float = null

New stock level if already read.

Return values
mixed

read_extra_data()

Read extra data associated with the product, like button text or product URL for external products.

protected read_extra_data( &$product : WC_Product ) : mixed
Parameters
$product : WC_Product

Product object.

Tags
since
Return values
mixed

read_visibility()

Convert visibility terms to props.

protected read_visibility( &$product : WC_Product ) : mixed

Catalog visibility valid values are 'visible', 'catalog', 'search', and 'hidden'.

Parameters
$product : WC_Product

Product object.

Tags
since
Return values
mixed

update_post_meta()

Helper method that updates all the post meta for a product based on it's settings in the WC_Product class.

protected update_post_meta( &$product : WC_Product [, $force : bool = false ] ) : mixed
Parameters
$product : WC_Product

Product object.

$force : bool = false

Force update. Used during create.

Tags
since
Return values
mixed

update_terms()

For all stored terms in all taxonomies, save them to the DB.

protected update_terms( &$product : WC_Product [, $force : bool = false ] ) : mixed
Parameters
$product : WC_Product

Product object.

$force : bool = false

Force update. Used during create.

Tags
since
Return values
mixed

update_visibility()

Update visibility terms based on props.

protected update_visibility( &$product : WC_Product [, $force : bool = false ] ) : mixed
Parameters
$product : WC_Product

Product object.

$force : bool = false

Force update. Used during create.

Tags
since
Return values
mixed

update_attributes()

Update attributes which are a mix of terms and meta data.

protected update_attributes( &$product : WC_Product [, $force : bool = false ] ) : mixed
Parameters
$product : WC_Product

Product object.

$force : bool = false

Force update. Used during create.

Tags
since
Return values
mixed

update_downloads()

Update downloads.

protected update_downloads( &$product : WC_Product [, $force : bool = false ] ) : bool
Parameters
$product : WC_Product

Product object.

$force : bool = false

Force update. Used during create.

Tags
since
Return values
bool

If updated or not.

update_version_and_type()

Make sure we store the product type and version (to track data changes).

protected update_version_and_type( &$product : WC_Product ) : mixed
Parameters
$product : WC_Product

Product object.

Tags
since
Return values
mixed

get_on_sale_products()

Returns an array of on sale products, as an array of objects with an ID and parent_id present. Example: $return[0]->id, $return[0]->parent_id.

public get_on_sale_products( ) : array
Tags
since
Return values
array

get_featured_product_ids()

Returns a list of product IDs ( id as key => parent as value) that are featured. Uses get_posts instead of wc_get_products since we want some extra meta queries and ALL products (posts_per_page = -1).

public get_featured_product_ids( ) : array
Tags
since
Return values
array

get_product_id_by_sku()

Return product ID based on SKU.

public get_product_id_by_sku( $sku : string ) : int
Parameters
$sku : string

Product SKU.

Tags
since
Return values
int

get_starting_sales()

Returns an array of IDs of products that have sales starting soon.

public get_starting_sales( ) : array
Tags
since
Return values
array

get_ending_sales()

Returns an array of IDs of products that have sales which are due to end.

public get_ending_sales( ) : array
Tags
since
Return values
array

find_matching_product_variation()

Find a matching (enabled) variation within a variable product.

public find_matching_product_variation( $product : WC_Product [, $match_attributes : array = array() ] ) : int
Parameters
$product : WC_Product

Variable product.

$match_attributes : array = array()

Array of attributes we want to try to match.

Tags
since
Return values
int

Matching variation ID or 0.

create_all_product_variations()

Creates all possible combinations of variations from the attributes, without creating duplicates.

public create_all_product_variations( $product : WC_Product [, $limit : int = -1 ] ) : int
Parameters
$product : WC_Product

Variable product.

$limit : int = -1

Limit the number of created variations.

Tags
since
todo

Add to interface in 4.0.

Return values
int

Number of created variations.

sort_all_product_variations()

Make sure all variations have a sort order set so they can be reordered correctly.

public sort_all_product_variations( $parent_id : int ) : mixed
Parameters
$parent_id : int

Product ID.

Return values
mixed

get_related_products()

Return a list of related products (using data like categories and IDs).

public get_related_products( $cats_array : array , $tags_array : array , $exclude_ids : array , $limit : int , $product_id : int ) : array
Parameters
$cats_array : array

List of categories IDs.

$tags_array : array

List of tags IDs.

$exclude_ids : array

Excluded IDs.

$limit : int

Limit of results.

$product_id : int

Product ID.

Tags
since
Return values
array

get_related_products_query()

Builds the related posts query.

public get_related_products_query( $cats_array : array , $tags_array : array , $exclude_ids : array , $limit : int ) : array
Parameters
$cats_array : array

List of categories IDs.

$tags_array : array

List of tags IDs.

$exclude_ids : array

Excluded IDs.

$limit : int

Limit of results.

Tags
since
Return values
array

set_product_stock()

Update a product's stock amount directly in the database.

protected set_product_stock( $product_id_with_stock : int , $stock_quantity : int|float|null ) : mixed

Updates both post meta and lookup tables. Ignores manage stock setting on the product.

Parameters
$product_id_with_stock : int

Product ID.

$stock_quantity : int|float|null

Stock quantity.

Return values
mixed

update_product_stock()

Update a product's stock amount directly.

public update_product_stock( $product_id_with_stock : int [, $stock_quantity : int|float|null = null ] [, $operation : string = 'set' ] ) : int|float

Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues). Ignores manage stock setting on the product and sets quantities directly in the db: post meta and lookup tables. Uses locking to update the quantity. If the lock is not acquired, change is lost.

Parameters
$product_id_with_stock : int

Product ID.

$stock_quantity : int|float|null = null

Stock quantity.

$operation : string = 'set'

Set, increase and decrease.

Tags
since

this supports set, increase and decrease.

Return values
int|float

New stock level.

update_product_sales()

Update a product's sale count directly.

public update_product_sales( $product_id : int [, $quantity : int|null = null ] [, $operation : string = 'set' ] ) : mixed

Uses queries rather than update_post_meta so we can do this in one query for performance.

Parameters
$product_id : int

Product ID.

$quantity : int|null = null

Quantity.

$operation : string = 'set'

set, increase and decrease.

Tags
since

this supports set, increase and decrease.

Return values
mixed

update_average_rating()

Update a products average rating meta.

public update_average_rating( $product : WC_Product ) : mixed
Parameters
$product : WC_Product

Product object.

Tags
since
todo

Deprecate unused function?

Return values
mixed

update_review_count()

Update a products review count meta.

public update_review_count( $product : WC_Product ) : mixed
Parameters
$product : WC_Product

Product object.

Tags
since
todo

Deprecate unused function?

Return values
mixed

get_shipping_class_id_by_slug()

Get shipping class ID by slug.

public get_shipping_class_id_by_slug( $slug : string ) : int|false
Parameters
$slug : string

Product shipping class slug.

Tags
since
Return values
int|false

get_products()

Returns an array of products.

public get_products( [ $args : array = array() ] ) : array|object
Parameters
$args : array = array()

Args to pass to WC_Product_Query().

Tags
see
Return values
array|object

search_products()

Search product data for a term and return ids.

public search_products( $term : string [, $type : string = '' ] [, $include_variations : bool = false ] [, $all_statuses : bool = false ] [, $limit : null|int = null ] [, $include : null|array = null ] [, $exclude : null|array = null ] ) : array
Parameters
$term : string

Search term.

$type : string = ''

Type of product.

$include_variations : bool = false

Include variations in search or not.

$all_statuses : bool = false

Should we search all statuses or limit to published.

$limit : null|int = null

Limit returned results. @since 3.5.0.

$include : null|array = null

Keep specific results. @since 3.6.0.

$exclude : null|array = null

Discard specific results. @since 3.6.0.

Return values
array

of ids

get_product_type()

Get the product type based on product ID.

public get_product_type( $product_id : int ) : bool|string
Parameters
$product_id : int

Product ID.

Tags
since
Return values
bool|string

reviews_allowed_query_where()

Add ability to get products by 'reviews_allowed' in WC_Product_Query.

public reviews_allowed_query_where( $where : string , $wp_query : WP_Query ) : string
Parameters
$where : string

Where clause.

$wp_query : WP_Query

WP_Query instance.

Tags
since
Return values
string

get_wp_query_args()

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

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

query vars from a WC_Object_Query.

Tags
since
Return values
array

query()

Query for Products matching specific criteria.

public query( $query_vars : array ) : array|object
Parameters
$query_vars : array

Query vars from a WC_Product_Query.

Tags
since
Return values
array|object

get_data_for_lookup_table()

Get data to save to a lookup table.

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

ID of object to update.

$table : string

Lookup table name.

Tags
since
Return values
array

get_primary_key_for_lookup_table()

Get primary key name for lookup table.

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

Lookup table name.

Tags
since
Return values
string

get_term_ids()

Get and store terms from a taxonomy.

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

WC_Data object or object ID.

$taxonomy : string

Taxonomy name e.g. product_cat.

Tags
since
Return values
array

of terms

read_meta()

Returns an array of meta for an object.

public read_meta( &$object : WC_Data ) : array
Parameters
$object : WC_Data

WC_Data object.

Tags
since
Return values
array

delete_meta()

Deletes meta based on meta ID.

public delete_meta( &$object : WC_Data , $meta : stdClass ) : mixed
Parameters
$object : WC_Data

WC_Data object.

$meta : stdClass

(containing at least ->id).

Tags
since
Return values
mixed

add_meta()

Add new piece of meta.

public add_meta( &$object : WC_Data , $meta : stdClass ) : int
Parameters
$object : WC_Data

WC_Data object.

$meta : stdClass

(containing ->key and ->value).

Tags
since
Return values
int

meta ID

update_meta()

Update meta.

public update_meta( &$object : WC_Data , $meta : stdClass ) : mixed
Parameters
$object : WC_Data

WC_Data object.

$meta : stdClass

(containing ->id, ->key and ->value).

Tags
since
Return values
mixed

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
Return values
array

Array elements: table, object_id_field, meta_id_field

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( $key : string ) : string
Parameters
$key : string

Prefix to be added to meta keys.

Tags
since
Return values
string

exclude_internal_meta_keys()

Callback to remove unwanted meta data.

protected exclude_internal_meta_keys( $meta : object ) : bool
Parameters
$meta : object

Meta object to check if it should be excluded or not.

Return values
bool

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( $object : WC_Data , $meta_key_to_props : array [, $meta_type : string = '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
array

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

update_or_delete_post_meta()

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

protected update_or_delete_post_meta( $object : WC_Data , $meta_key : string , $meta_value : mixed ) : 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

Added to prevent empty meta being stored unless required.

Return values
bool

True if updated/deleted.

parse_date_for_wp_query()

Map a valid date query var to WP_Query arguments.

public parse_date_for_wp_query( $query_var : mixed , $key : string [, $wp_query_args : array = 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
Return values
array

Modified $wp_query_args

get_internal_meta_keys()

Return list of internal meta keys.

public get_internal_meta_keys( ) : array
Tags
since
Return values
array

get_valid_search_terms()

Check if the terms are suitable for searching.

protected get_valid_search_terms( $terms : array ) : 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
Return values
array

Terms that are not stopwords.

get_search_stopwords()

Retrieve stopwords used when parsing search terms.

protected get_search_stopwords( ) : array
Tags
since
Return values
array

Stopwords.

update_lookup_table()

Update a lookup table for an object.

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

ID of object to update.

$table : string

Lookup table name.

Tags
since
Return values
null

delete_from_lookup_table()

Delete lookup table data for an ID.

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

ID of object to update.

$table : string

Lookup table name.

Tags
since
Return values
mixed
Back to the top