WC_Product_Data_Store_CPT::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.
Method of the class: WC_Product_Data_Store_CPT{}
No Hooks.
Returns
Array.
Usage
$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT(); $WC_Product_Data_Store_CPT->get_on_sale_products();
Changelog
| Since 3.0.0 | Introduced. |
WC_Product_Data_Store_CPT::get_on_sale_products() WC Product Data Store CPT::get on sale products code WC 10.3.3
public function get_on_sale_products() {
global $wpdb;
$exclude_term_ids = array();
$outofstock_join = '';
$outofstock_where = '';
$non_published_where = '';
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids[ ProductStockStatus::OUT_OF_STOCK ] ) {
$exclude_term_ids[] = $product_visibility_term_ids[ ProductStockStatus::OUT_OF_STOCK ];
}
if ( count( $exclude_term_ids ) ) {
$outofstock_join = " LEFT JOIN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( " . implode( ',', array_map( 'absint', $exclude_term_ids ) ) . ' ) ) AS exclude_join ON exclude_join.object_id = id';
$outofstock_where = ' AND exclude_join.object_id IS NULL';
}
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
return $wpdb->get_results(
"
SELECT posts.ID as id, posts.post_parent as parent_id
FROM {$wpdb->posts} AS posts
INNER JOIN {$wpdb->wc_product_meta_lookup} AS lookup ON posts.ID = lookup.product_id
$outofstock_join
WHERE posts.post_type IN ( 'product', 'product_variation' )
AND posts.post_status = 'publish'
AND lookup.onsale = 1
$outofstock_where
AND posts.post_parent NOT IN (
SELECT ID FROM `$wpdb->posts` as posts
WHERE posts.post_type = 'product'
AND posts.post_parent = 0
AND posts.post_status != 'publish'
)
GROUP BY posts.ID
"
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}