wc_get_products and WC_Product_Query
wc_get_products
and WC_Product_Query
provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance. This is the best-practices way for plugin and theme developers to retrieve multiple products. wc_get_products
and WC_Product_Query
are similar to WordPress get_posts
and WP_Query
. Just like those, you pass in an array of arguments defining the criteria for the search.
Here are a few examples:
// Get downloadable products created in the year 2016.
$products = wc_get_products( array(
'downloadable' => true,
'date_created' => '2016-01-01...2016-12-31`,
) );
// Get 10 most recent product ids in date descending order.
$query = new WC_Product_Query( array(
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$products = $query->get_products();
// Get products containing a specific SKU.
// Does partial matching, so this will get products with SKUs "PRDCT-1", "PRDCT-2", etc.
$query = new WC_Product_Query();
$query->set( 'sku', 'PRDCT' );
$products = $query->get_products();
get_query_vars() - Get an array of all of the current query variables set on the query object.
get( string $query_var, mixed $default = '' ) - Get the value of a query variable or the default if the query variable is not set.
set( string $query_var, mixed $value ) - Set a query variable to a value.
get_products() - Get all products matching the current query variables.
status
Accepts a string: one of 'pending', 'processing', 'on-hold', 'completed', 'refunded, 'failed', 'cancelled', or a custom order status.
// Get orders on hold.
$args = array(
'status' => 'on-hold',
);
$orders = wc_get_orders( $args );
type
TODO
include
TODO
exclude
TODO
parent
TODO
parent_exclude
TODO
limit
TODO
page
TODO
paginate
TODO
offset
TODO
order
TODO
orderby
TODO
return
TODO
slug
TODO
sku
TODO
tag
TODO
category
TODO
weight, length, width, height
TODO
price, regular_price, sale_price
TODO
total_sales
TODO
virtual, downloadable, featured, sold_individually, backorders, manage_stock, reviews_allowed
TODO
visibility
TODO
stock_quantity
TODO
stock_status
TODO
tax_status
TODO
tax_class
TODO
shipping_class
TODO
upsell_ids, cross_sell_ids
TODO
attributes
TODO
default_attributes
TODO
menu_order
TODO
download_limit
TODO
download_expiry
TODO
average_rating
TODO
review_count
TODO
date_created, date_modified, date_on_sale_from, date_on_sale_to
Accepts a string. Date queries use a standard format:
YYYY-MM-DD
- Matches on products during that one day in site timezone.
>YYYY-MM-DD
- Matches on products after that one day in site timezone.
>=YYYY-MM-DD
- Matches on products during or after that one day in site timezone.
<YYYY-MM-DD
- Matches on products before that one day in site timezone.
<=YYYY-MM-DD
- Matches on products during or before that one day in site timezone.
YYYY-MM-DD...YYYY-MM-DD
- Matches on products during or in between the days in site timezone.
TIMESTAMP
- Matches on products during that one second in UTC timezone.
>TIMESTAMP
- Matches on products after that one second in UTC timezone.
>=TIMESTAMP
- Matches on products during or after that one second in UTC timezone.
<TIMESTAMP
- Matches on products before that one second in UTC timezone.
<=TIMESTAMP
- Matches on products during or before that one second in UTC timezone.
TIMESTAMP...TIMESTAMP
- Matches on products during or in between the seconds in UTC timezone.
TODO
WooCommerce is an open source commerce platform built for WordPress and lovingly crafted by Automattic and the WooCommerce community 💜. Come and work with us!
Contribution
- Set up development environment
- Our Git Flow
- SCSS and JS minification
- Naming conventions
- CSS SASS coding guidelines and naming conventions
- Critical Flows
- API Critical Flows
- String localisation guidelines
- Translating WooCommerce
- Deprecation in core
- Adding Actions and Filters
- Common Issues
- Writing high-quality testing instructions
Release Notes
- Release Testing Instructions
- 3.6.x notes/FAQ
- 2.6.x to 3.0.0 Developer Migration Notes
- Select2 fields not working in 3.0.x
- Thumbnail Image Regeneration in 3.3+
- Customizing image sizes in 3.3+
REST API and CLI
CRUD & Data Descriptions
- Database Description
- CRUD Objects in 3.0
- Order and Order Line Item Data
- Coupon Data
- Customer Data
- Product Data
- Data Stores
Internal APIs
Theming
- Enabling product gallery features (zoom, swipe, lightbox)
- Template File Guidelines for Devs and Theme Authors
Examples / Guides