Get product IDS that are in sale for woocommerce product query
-
Hello,
I am trying to display the discounted products on my sale page, the sale page is simply a filter on the woocommerce_product_query hook.
Woocommerce has a function which returns all the product ID’s that are on sale. I’ve tried to loop through all the products with the advanced_woo_discount_rules_get_product_discount_price_from_custom_price filter (see bottom), but that results in a 500 error.
function bd_change_product_query( $q ){ if($_GET['filter'] == 'sale'){ $id_s = wc_get_product_ids_on_sale(); $q->set( 'post__in', (array) $id_s ); } } add_action( 'woocommerce_product_query', 'bd_change_product_query' );
Looping through all products:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 ); $products = get_posts( $args ); foreach($products as $p){ $discounted_price = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', false, $product, 1, 0, 'all', true); $wooco_discounted_price = $product->get_sale_price(); if ($discounted_price !== false) { if ($discounted_price['discounted_price'] > 0) { // woo discount rule, here I want to push the ID to a array } elseif ($wooco_discounted_price > 0) { // woocommerce discount } } }
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Get product IDS that are in sale for woocommerce product query’ is closed to new replies.