0

I use a customized shop page in my site. There I use the "Product Loop" component of WPBakery (former Visual Composer).

I want to change the "Recent" order to make the "Out of Stock" products do not show in this case, and I tried to put 'stock_status' => 'instock', in the code bellow, but it didn't work.

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
switch ($display) {
    case 'recent':
       $args = array(
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'stock_status'          => 'instock',
            'ignore_sticky_posts'   => 1,
            'posts_per_page'        => $count,
            'orderby'               => 'date',
            'order'                 => 'desc',
            'paged'                 => $paged,
            'meta_query'            => WC()->query->get_meta_query(),
        );
        break;

The result is the same with or without the line.

Another situation:

I want to change the "Top Rated" option to make the "Out of Stock" products appear at the end of the product loop. I have the follow code:

case 'top_rated':
       $args = array(
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'posts_per_page'        => $count,
            'order'                 => $order,
            'paged'                 => $paged,
            'meta_key'              => '_wc_average_rating',
            'orderby'               => 'meta_value_num',
            'meta_query'            => WC()->query->get_meta_query(),
            'tax_query'             => WC()->query->get_tax_query(),
        );
        break;

I would need to use a query with 2 arguments, the top rated and the stock_status. How can I create the query in the code above?

My environment:
Theme Jupiter Version 6.1.1
WPBakery 5.4.5.1
WooCommerce Version 3.3.4
WordPress 4.9.4

Sorry, my knowledge in PHP is newbie...

Diego Gaona
  • 498
  • 6
  • 24

2 Answers2

1

For the 'recent' case:

//RAS
//creates a variable with the standard meta query of woocommerce
$meta_query  = WC()->query->get_meta_query();
/*adds custom metakey to variable above with argument to display all that have the field
'_stock_status' with value other than outofstock
*/
$meta_query[] =  array(
    'relation' => 'AND',
    array(
        'key'     => '_stock_status',
        'value'   => 'outofstock',
        'compare'   => '!='
    ),

);

   $args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page'        => $count,
        'orderby'               => 'date',
        'order'                 => 'desc',
        'paged'                 => $paged,
        'meta_query'            =>  $meta_query,//use variable created as an argument

    );
    break;
Erty Seidohl
  • 4,487
  • 3
  • 33
  • 45
1

Para o segundo caso. case 'top_rated':

    //RAS
    //cria uma variável com a meta query padrao do woocommerce
    $meta_query  = WC()->query->get_meta_query();  
    /*cria instancias para as 2 metakeys(array) para utilizar como argumento no orderby, e adiciona a variável acima.*/         

    $meta_query = array(
    'relation' => 'AND',
    'rating' => array('key' => '_wc_average_rating','compare' => 'EXISTS',),
    'stockstatus' => array('key' => '_stock_status','compare' => 'EXISTS',), 
    );     

   $args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page'        => $count,
        //'order'                 => $order,
        'paged'                 => $paged,
        //'meta_key'            => '_wc_average_rating',
        //'orderby'             => 'meta_value_num',
        'meta_query'            => $meta_query,//usa a variavel criada como argumento
        'tax_query'             => WC()->query->get_tax_query(),
         'orderby' => array('rating'=>'DESC','stockstatus'=>'ASC',),//usa as instancias criadas como argumento para orderby.
    );

    break;