0

I know I can modify the query to show only the on sale products in the loop like this

add_action('woocommerce_product_query','show_only_on_sale_products');
function show_only_on_sale_products( $q )
{
    $product_ids_on_sale = wc_get_product_ids_on_sale();
    $q->set( 'post__in', $product_ids_on_sale );
}

How can I make a button where I call this? So I can toggle with it.

PS: I know I can use a shortcode for it but I don't like this solution:

[products limit="4" orderby="popularity" class="quick-sale" on_sale="true" ]
user123
  • 37
  • 8

1 Answers1

0

Since you already have your PHP function here then if I am correct, all you want is to execute this function on a button click, if yes, kindly check Execute PHP function with onclick post.

I believe it will help you. Have a good day.

MrEbabi
  • 740
  • 5
  • 16
  • Not really, what I need is after the button is clicked the product part of the page is reloaded and the hook is added to the query that will filter the data. I need ajax for this but I don’t know what url and action to pass. – user123 Oct 30 '19 at 11:40
  • I want to add a button in the header that will filter my products using ajax. – user123 Oct 30 '19 at 11:46