M
M
mipfikus2020-10-22 14:25:35
WordPress
mipfikus, 2020-10-22 14:25:35

How can I change the order in which products are displayed by default?

Now woocommerce displays products by price from small to large, but you need to display them by the value of the attribute "diametr" (number) in ascending order.
I found such a piece of code on the Internet, but how should I wrap it so that it changes the order of my goods?

$args['order'] = 'ASC';
$args['meta_key'] = 'diametr';
$args['orderby'] = 'meta_value_num';

Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pychev Anatoly, 2020-10-22
@mipfikus

I found such a piece of code on the Internet, but how should I wrap it so that it changes the order of my goods?
The answer is NO. Because this is a metafield sorting control module, and you need it by attribute value.
Alternatively, you need to write a block of code that will translate your attributes into meta fields and then sort using your piece of code. The task is not trivial, but solvable.
Here is an example algorithm
  • For each attribute value (term in the wp_terms table) add a meta field (in the term meta field table) with sort order value (0,1,2,3,...)
  • Connect to the save product hook and read the value of the attribute meta field for the product being saved
  • Store this value in the product meta field
  • Sort by metafield

Well, something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question