1

I'm trying to add a product archive widget in Elementor but in this widget specifically must hide "Out of stock" products.

I try to use the following code`s but I have not succeeded, because they doesn't work if Elementor widget is AJAX.

If the widget is not AJAX, then it`s fine (they are working).

How to modify this code`s to work with AJAX?

1). With a meta query:

add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
    if( ! is_admin() ) {
        $query_args['meta_query'][] = array(
            'key'     => '_stock_status',
            'value'   => 'outofstock',
            'compare' => '!='
        );
    }
    return $query_args;
}

2). Or with a tax query:

add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
    if( ! is_admin() ) {
        $query_args['tax_query'][] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => array('outofstock'),
            'operator' => 'NOT IN'
        );
    }
    return $query_args;
}

I found this codes from another stack question: Answered by @LoicTheAztec

Acidburns
  • 131
  • 3
  • 13

0 Answers0