A
A
Artem2015-09-27 23:48:56
WordPress
Artem, 2015-09-27 23:48:56

WooCommerce (WordPress) How to sort "Similar Products"?

Good day. Please tell me how to select sorting "Similar products".
I want to make "Similar Products" show options that are similar to the price of an already selected product, that is, similar prices are shown.
Please tell me how can this be done?
Here is the code of the function, which, as I understand it, takes data from the database:

public function shop_item_tab_similar( $post_id, $query = false ){

    if( !$query ){

      $cats = wp_get_post_terms( $post_id, 'product_cat' );
      $cat  = $cats[0];

      $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' => '0',
                'compare' => '>',
            ),
            array(
                'key' => '_stock_status',
                'value' => 'instock',
                'compare' => '=',
            )
        )
      );

    }

    $items = $this->get_shop_items_by_query( $query );

    $similar = $this->application->get_include_contents( $this->blocksPath.'/product-tab-similar.php', array( 'similar' => $items ) );

    return array(
      'content' => $similar,
      'section' => "data-query='".json_encode($query)."'"
    );
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WP Panda, 2015-09-28
@Sice

if( !$query ){
$prise = get_post_meta($post_id,'_price',true);
      $query = array(
        'posts_per_page' 	=> 4,
        'paged' 			=> 1,
        'post_status' 		=> 'publish',
        'post_type' 		=> 'product',
        'exclude' 			=> $post_id,
        'meta_query' => array(
            array(
                'key' => '_price',
                'value' => $prise,
                'compare' => '=',
            ),
        )
      );

    }

A
Artem, 2015-09-28
@Sice

In order not to create a new topic, please tell me how to show the results not clearly equal prices, but approximately, with a difference of 5-10 thousand, for example?
This is how
the values ​​\u200b\u200bare displayed in discord at all. And I wanted similar products to be shown approximately equal in price in order.
For example, a product costs 71 thousand. Similar products show the product: 75 thousand, 73, 70, 69 and so on...
I don’t understand why it displays so strangely. When you click "More products" to show 4 more products - it displays values ​​much higher than the cost of the selected product, although the selection goes like Less (<)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question