Answer the question
In order to leave comments, you need to log in
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' => '=',
)
)
);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question