M
M
maxim5582022-02-06 20:08:21
WordPress
maxim558, 2022-02-06 20:08:21

How to sort products in category by nested custom fields in Woocommerce?

For products there is a custom field Position in Category - " cat_position " created in ACF. It has two nested fields "Category selection" ( cat_position_vybor ) and "Position number" ( cat_position_number ). In the product, it looks like this:
LQA0EW1.png

I.e. one product can belong to several categories, which are indicated in the "Category selection". And for each category, its own position is indicated in the "Position Number".

It turns out that for each product, the cat_position field is an array that looks something like this:

$cat_position = (
    [0] => Array
        (
            [cat_position_vybor] => 159
            [cat_position_number] => 37
        ),

    [1] => Array
        (
            [cat_position_vybor] => 160
            [cat_position_number] => 37
        ),

    [2] => Array
        (
            [cat_position_vybor] => 161
            [cat_position_number] => 37
        ),

    [3] => Array
        (
            [cat_position_vybor] => 162
            [cat_position_number] => 37
        ),
    ...)


Here " cat_position_vybor " is the id of the product categories.

In the category itself, through the shortcode, the positions corresponding to the category have already been displayed. Now we need to somehow sort the products by the nested custom field "*cat_position_number*".

So I understand that this needs to be done through the " pre_get_posts " hook and the " meta_query " parameter . But I don’t understand how to specify that it is sorted exactly by those " cat_position_number " that correspond to the desired " cat_position_vybor ", i.e. category in which we are now.

[1]: https://i.stack.imgur.com/GQQNd.png

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