Jump to content

Фильтр по цене в админ панели Simpla


Recommended Posts

Может кому пригодится, понадобилось сделать фильтр клиента по цене, найти товары у которых цена больше, меньше или равна.

 

в 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

 Вдруг кому пригодится.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...