Kami Posted October 13, 2023 Report Share Posted October 13, 2023 Может кому пригодится, понадобилось сделать фильтр клиента по цене, найти товары у которых цена больше, меньше или равна. в simpla/design/html/products.tpl {* По цене *} <form method="get"> <div id="price"> <input type="hidden" name="module" value="ProductsAdmin"> <select name="price_comparison"> <option value="" {if empty($price_comparison)}selected{/if}>Выберите</option> <option value="equal" {if $price_comparison == "equal"}selected{/if}>Равно</option> <option value="less" {if $price_comparison == "less"}selected{/if}>Меньше</option> <option value="greater" {if $price_comparison == "greater"}selected{/if}>Больше</option> </select> <input class="price_input" type="text" name="price" placeholder="Введите цену" value="{$price|escape}" /> <input class="price_button" type="submit" value="Найти"/> </div> </form> В simpla/design/css/style.css #price { display: flex; flex-direction: column; gap: 15px; padding: 15px 15px; border: 1px solid #343434; margin-bottom: 30px; } В ProductsAdmin.php $price_comparison = $this->request->get('price_comparison'); $price = $this->request->get('price'); if (!empty($price)) { $filter['price'] = $price; $this->design->assign('price', $price); } if (!empty($price_comparison)) { $filter['price_comparison'] = $price_comparison; $this->design->assign('price_comparison', $price_comparison); } В Api/products.php в 2 функции get_products и count_products ставим новый фильтр /*фильтр по цене*/ $price_filter = ''; /*фильтр по цене*/ Далее сам фильтр /*фильтр по цене*/ if (isset($filter['price_comparison']) && isset($filter['price'])) { $price_comparison = $filter['price_comparison']; $price_value = intval($filter['price']); switch ($price_comparison) { case 'equal': $price_filter = $this->db->placehold('AND (SELECT 1 FROM __variants pv WHERE pv.product_id=p.id AND pv.price = ? LIMIT 1)', $price_value); break; case 'less': $price_filter = $this->db->placehold('AND (SELECT 1 FROM __variants pv WHERE pv.product_id=p.id AND pv.price < ? LIMIT 1)', $price_value); break; case 'greater': $price_filter = $this->db->placehold('AND (SELECT 1 FROM __variants pv WHERE pv.product_id=p.id AND pv.price > ? LIMIT 1)', $price_value); break; } } /*фильтр по цене*/ и в общем $query добавляем $price_filter Вдруг кому пригодится. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.