A
A
Artem2015-09-29 11:56:52
WordPress
Artem, 2015-09-29 11:56:52

WooCommerce (WordPress) How to show "Related Products" by price?

Good day. Worth WordPress and WooCommerce. There is a code that should show "Similar Products" by price, but as a result, there is a strong spread both up and down, although as far as I understand, there should not be a sample at all.
The code:

if( !$query ){

      $cats = wp_get_post_terms( $post_id, 'product_cat' );
      $cat  = $cats[0];
      $priсes = get_post_meta($post_id,'_price',true);

      $query = array(
        'posts_per_page' 	=> 4, 
        'paged' 			=> 1,
        'post_status' 		=> 'publish',
        'post_type' 		=> 'product',
        'exclude' 			=> $post_id,
        'tax_query' => array(
          array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $cat->term_id
          )
        ),
        'meta_query' => array(
          array(
            'key' => '_price',
            'value' => $priсes,
            'compare' => '<',
          ),
            array(
                'key' => '_stock_status',
                'value' => 'instock',
                'compare' => '=',
            )
        )
      );

    }

As a result, the product is displayed not sorted by price, in a scatter, and for some reason it also shows Products with a higher price. Although there is a condition 'compare' => '<'.
Please tell me how to display products in "Similar Products" with a difference of +/- 5-10 thousand and only them?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question