0
Я добавил атрибуты для фильтрации продуктов на основе их выбора. Например, у меня есть поле с именем «плитка» как «да» или «нет». Если пользователь отметит «да», будут показаны все продукты с атрибутом «да». Все продукты с атрибутом «нет» не отображаются.
Как я могу отобразить все продукты, которые не отфильтрованы НИЖЕ отфильтрованного запроса (т. е. показать все продукты с надписью «Нет» под фильтром продуктов)?
Я думаю, что внутри моего файла function.php запрашиваются отфильтрованные продукты следующим образом:
function sort_products_by_positive_attributes($query) {
if (!is_admin() && $query->is_main_query() && is_woocommerce()) {
// First, you would need to get all products along with their positive attributes count
$products_with_scores = [];
if (!empty($query->posts)) {
foreach ($query->posts as $post) {
$product = wc_get_product($post->ID);
$score = get_product_positive_attributes_count($product);
$products_with_scores[$post->ID] = $score;
}
}else {
}
// Then you would sort products by their score
uasort($products_with_scores, function($a, $b) {
return $b - $a;
});
// After sorting, modify the query to order by the IDs in the sorted order
$sorted_ids = array_keys($products_with_scores);
$query->set('orderby', 'post__in');
$query->set('post__in', $sorted_ids);
}
}
//add_action('pre_get_posts', 'sort_products_by_positive_attributes');
function get_product_positive_attributes_count($product) {
// You would implement the logic here to count the number of positive attributes
$score = 0;
// for each attribute, if it's positive, increment $score
return $score;
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... elow-query
Фильтрация с помощью атрибутов – как отобразить все незапрошенные продукты под запросом ⇐ Php
Кемеровские программисты php общаются здесь
1732813101
Anonymous
0
Я добавил атрибуты для фильтрации продуктов на основе их выбора. Например, у меня есть поле с именем «плитка» как «да» или «нет». Если пользователь отметит «да», будут показаны все продукты с атрибутом «да». Все продукты с атрибутом «нет» не отображаются.
Как я могу отобразить все продукты, которые не отфильтрованы НИЖЕ отфильтрованного запроса (т. е. показать все продукты с надписью «Нет» под фильтром продуктов)?
Я думаю, что внутри моего файла function.php запрашиваются отфильтрованные продукты следующим образом:
function sort_products_by_positive_attributes($query) {
if (!is_admin() && $query->is_main_query() && is_woocommerce()) {
// First, you would need to get all products along with their positive attributes count
$products_with_scores = [];
if (!empty($query->posts)) {
foreach ($query->posts as $post) {
$product = wc_get_product($post->ID);
$score = get_product_positive_attributes_count($product);
$products_with_scores[$post->ID] = $score;
}
}else {
}
// Then you would sort products by their score
uasort($products_with_scores, function($a, $b) {
return $b - $a;
});
// After sorting, modify the query to order by the IDs in the sorted order
$sorted_ids = array_keys($products_with_scores);
$query->set('orderby', 'post__in');
$query->set('post__in', $sorted_ids);
}
}
//add_action('pre_get_posts', 'sort_products_by_positive_attributes');
function get_product_positive_attributes_count($product) {
// You would implement the logic here to count the number of positive attributes
$score = 0;
// for each attribute, if it's positive, increment $score
return $score;
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79234764/filtering-with-attributes-how-to-display-all-non-queried-products-below-query[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия