Section Template for listing products under $X dollars with Elementor

[ad_1]

Hello, I am posting this with a hope that someone can guide me to the right directions. I need to show a list of products that are less than a certain amount (e.g, $6 in this example). My understanding is that there is no direct way of executing the query within elementor widgets. All my readings point me to that I have to create a function and hook the function with add\_action(‘elementor/query/query-id’). Plus, I need to add the query id in the proper widget. I looked the Woo ‘Products’ widget and the widget does not provide a place where I can enter a query\_id. I do see the entry field ‘query-id” in ‘Posts’ widget, I do see the field ‘query-id’ in Posts widget and entered the ‘filter\_by\_price’ as the query-id. I also see a field called ‘Source’ in the Posts widget and selected ‘current query’ and also tried “manual selection”. All this was done within a new section template and inserted the section into one of ‘Product Archive’ template pages. Long story short, it doesn’t work. With the selection of ‘current query”, it seems to show all products likely because the “current” query for the Product Archives page is to select all products I guess. I have spent near 10 hours already and clueless what to try next. I hope the explanation makes sense and if not, I apologize. I have a feeling that I am close but at the same time, I feel like this is an overkill for a simple task. My environment is Woocommerce and Elementor Pro.

Here is my plugin file code snippet

<?php
// Function to filter products by price
function filter\_products\_by\_price($query) {
if ($query->get(‘elementor\_query\_id’) === ‘filter\_by\_price’) {
$max\_price = 6; // Set the maximum price you want to filter by

// Get current meta query
$meta\_query = $query->get(‘meta\_query’);

// If there is no meta query when this filter runs, it should be initialized as an empty array.
if (!$meta\_query) {
$meta\_query = \[\];
}

// Append our meta query to filter by price
$meta\_query\[\] = \[
‘key’ => ‘\_price’,
‘value’ => $max\_price,
‘compare’ => ‘<=’,
‘type’ => ‘NUMERIC’,
\];

$query->set(‘meta\_query’, $meta\_query);
}
}
add\_action(‘elementor/query/filter\_by\_price’, ‘filter\_products\_by\_price’);

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer