Answer the question
In order to leave comments, you need to log in
How to get the sort criterion in a general loop?
Good afternoon
I was given the task of displaying products in the store in a certain order. And in each category of goods the procedure is different. For example: There is a category of sportswear and it contains such products as T-shirts, T-shirts, gloves, shorts. The task is to display on the page of this category first all the T-shirts, then all the shorts, then the rest. But these same products are also in other categories, and in other categories, T-shirts will no longer be in the first place.
After a lot of googling, I decided that the most suitable option is to do it as described here .
I will quote
This is not difficult to do, but requires knowledge of php at least. The point is to change the archive-product.php template. You need to change the loop in this template and disable wc_get_template_part(). It is necessary not to display content in the while loop, but to form an associative array. Roughly speaking, we have a list of all goods (records). In the loop, we go through all the products. Each item has a label. And write to the array, where the cell key is just the product label. As a result, the array will look like array('metka_tovara1' => array('array of products'), 'metka_tovara2' => array('array of products2'), 'metka_tovara3' => array('array of products3')). Products array stores products. For each product there is a list of required fields for output - link, image, title, description, prices, etc..
This method will not affect the standard work of woocommerce and the filter. Perhaps it is written difficult, but in more detail - it is to write the code.
Answer the question
In order to leave comments, you need to log in
And now the QUESTION: How do I, when I am in the main cycle for displaying products or before it, find out if the sorting was default (default) or the user selected something?
- 1 option, see $_REQUEST. When sorting is used, there is something there.
- Option 2 to look at global wp_query with different sorting, find out what changes there.
Here you need to estimate how many products in each category and how many categories. I will suggest the easiest way to implement it through custom fields. For each product card, you need to create a custom field called 'order_$term_id'. Those. depending on the number of headings in which the product is located, there will be a number of such fields. You can write a script that will create these fields itself depending on the number of selected categories. In these fields you need to put down the number of positions. For example, T-shirts will have a number from 0 to 100, shorts will have a number from 100 to 200, and so on. It's not important here and you have to do it right. You can put the same number for all shorts or a different one, depending on whether you need to sort additionally within the group of shorts.
It turns out that all category cards with id 16 will have a custom field order_16 with values inside. It is important that at least 0 is there.
Well, the last step is to intercept the wp_query request while in the category and change the sorting
$args = array(
'meta_key' => 'order_16',
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
add_action( 'pre_get_posts', 'modify_main_query' );
function filters_modify_main_query( $query ) {
return $query;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question