Сортировка по названию и цене. Storefront.class.php
В шаблонеPHP:// Функция возвращает товары function get_products($ids = null, $categories = null, $brand_id = null, $start_item=null, $filter=null, $hit=null) { if (!empty($_GET['desc'])) { $desc = $_GET['desc']; } else { $desc = 'ASC'; } if (!empty($_GET['sort']) && ($_GET['sort']) == 'products.model') { $sort = $_GET['sort']; } else { $sort = 'products_variants.price'; } if (!empty($_REQUEST['min_price']) && is_numeric($_REQUEST['min_price'])) { $min_price = $_REQUEST['min_price']; } else { $min_price = '0'; } if (!empty($_REQUEST['max_price']) && is_numeric($_REQUEST['max_price'])) { $max_price = $_REQUEST['max_price']; } else { $max_price = '9999999'; } $this->smarty->assign('sort', $sort); $this->smarty->assign('desc', $desc); $this->smarty->assign('min_price', $min_price); $this->smarty->assign('max_price', $max_price); // Если заданы id $id_filter = ''; if (is_array($ids)) { foreach ($ids as $k=>$id) { $ids[$k]=intval($id); } $id_filter = is_null($ids)?"":"AND (products.product_id in(".join($ids, ',')."))"; } // Если задан бренд, добавляем фильт по бренду $brand_filter = is_null($brand_id)?"":"AND brands.brand_id = $brand_id"; // Если задан хит, добавляем фильт по хитам $hit_filter = is_null($hit)?"":"AND products.hit = $hit"; // Если задан бренд, добавляем фильт по бренду $limit = is_null($start_item)?"":"LIMIT $start_item, $this->items_per_page"; // Фильтр по свойствам $properties_filter = ''; if($filter) { foreach($filter as $property=>$value) $properties_filter .= sql_placeholder(" AND products.product_id in (SELECT properties_values.product_id FROM properties_values WHERE properties_values.product_id = products.product_id AND properties_values.value=? AND properties_values.property_id=?) ", $value, $property); } //С дополнительными категориями // Если задана категория, добавляем фильт по категории $category_filter = is_null($categories)?"":"AND ( (categories.category_id in(".join($categories, ',').") ) OR (products_categories.category_id in(".join($categories, ',').") ) )"; $query = "SELECT products.product_id, products.url, products.category_id, products.brand_id, products.model, products.description, products.body, products.hit, products.order_num, products.small_image, products.large_image, DATE_FORMAT(products.created, '%Y-%m-%d') as created, DATE_FORMAT(products.modified, '%Y-%m-%d') as modified, products.enabled, brands.name as brand, brands.url as brand_url, categories.single_name as category, categories.url as category_url, categories.image as category_image, products_variants.sku FROM products LEFT JOIN categories ON categories.category_id = products.category_id LEFT JOIN brands ON products.brand_id = brands.brand_id LEFT JOIN products_categories ON products.product_id = products_categories.product_id LEFT JOIN products_variants ON products.product_id = products_variants.product_id WHERE categories.enabled=1 and products.enabled=1 and products_variants.price>$min_price and products_variants.price<$max_price $id_filter $category_filter $brand_filter $properties_filter $hit_filter GROUP BY products.product_id ORDER BY $sort $desc $limit"; $this->db->query($query); $temp_products = $this->db->results(); foreach($temp_products as $product) $products[$product->product_id] = $product; if(is_array($products)) { $ids = array_keys($products); // Если пользователь залогиен, применим сразу его скидку к ценам на товар $discount=isset($this->user->discount)?$this->user->discount:0; $query = sql_placeholder("SELECT products_variants.*, products_variants.price*(100-$discount)/100 as discount_price, products_variants.stock as stock, products_variants.name as variant_name FROM products_variants WHERE products_variants.product_id in (?@) AND products_variants.stock>0 AND products_variants.price>0 ORDER BY products_variants.position", $ids); $this->db->query($query); $variants = $this->db->results(); foreach($variants as $variant) { if(!empty($products[$variant->product_id])) { $products[$variant->product_id]->variants[]=$variant; } } } return $products; }
PHP:<div id="sort"> <span>Сортировать по:</span> {if $sort == 'products_variants.price'} {if $desc == 'ASC'} <a href='{if $category}catalog/{$category->url}/{elseif $brand}brands/{/if}{if $brand}{$brand->url}/{/if}{if $price_url}{$price_url}&{else}?{/if}desc=DESC{if $min_price}&min_price={$min_price}{/if}{if $max_price}&max_price={$max_price}{/if}' ><span id="sort_lnk_act">цене</span><img src="design/{$settings->theme}/images/dowm.gif" /></a> <a href='{if $category}catalog/{$category->url}/{elseif $brand}brands/{/if}{if $brand}{$brand->url}/{/if}{if $price_url}{$price_url}&{else}?{/if}desc=DESC{if $min_price}&min_price={$min_price}{/if}{if $max_price}&max_price={$max_price}{/if}&sort=products.model' ><span id="sort_lnk">названию</span></a> {else} <a href='{if $category}catalog/{$category->url}/{elseif $brand}brands/{/if}{if $brand}{$brand->url}/{/if}{if $price_url}{$price_url}&{else}?{/if}desc=ASC{if $min_price}&min_price={$min_price}{/if}{if $max_price}&max_price={$max_price}{/if}' ><span id="sort_lnk_act">цене</span><img src="design/{$settings->theme}/images/up.gif" /></a> <a href='{if $category}catalog/{$category->url}/{elseif $brand}brands/{/if}{if $brand}{$brand->url}/{/if}{if $price_url}{$price_url}&{else}?{/if}desc=ASC{if $min_price}&min_price={$min_price}{/if}{if $max_price}&max_price={$max_price}{/if}&sort=products.model' ><span id="sort_lnk">названию</span></a> {/if} {/if} {if $sort == 'products.model'} {if $desc == 'ASC'} <a href='{if $category}catalog/{$category->url}/{elseif $brand}brands/{/if}{if $brand}{$brand->url}/{/if}{if $price_url}{$price_url}&{else}?{/if}desc=DESC{if $min_price}&min_price={$min_price}{/if}{if $max_price}&max_price={$max_price}{/if}' ><span id="sort_lnk">цене</span></a> <a href='{if $category}catalog/{$category->url}/{elseif $brand}brands/{/if}{if $brand}{$brand->url}/{/if}{if $price_url}{$price_url}&{else}?{/if}desc=DESC{if $min_price}&min_price={$min_price}{/if}{if $max_price}&max_price={$max_price}{/if}&sort=products.model' ><span id="sort_lnk_act">названию</span><img src="design/{$settings->theme}/images/dowm.gif" /></a> {else} <a href='{if $category}catalog/{$category->url}/{elseif $brand}brands/{/if}{if $brand}{$brand->url}/{/if}{if $price_url}{$price_url}&{else}?{/if}desc=ASC{if $min_price}&min_price={$min_price}{/if}{if $max_price}&max_price={$max_price}{/if}' ><span id="sort_lnk">цене</span></a> <a href='{if $category}catalog/{$category->url}/{elseif $brand}brands/{/if}{if $brand}{$brand->url}/{/if}{if $price_url}{$price_url}&{else}?{/if}desc=ASC{if $min_price}&min_price={$min_price}{/if}{if $max_price}&max_price={$max_price}{/if}&sort=products.model' ><span id="sort_lnk_act">названию</span><img src="design/{$settings->theme}/images/up.gif" /></a> {/if} {/if} </div>
Попробовал сделать сортировку Вашим способом, получилось сортировка работает.
НО весь товар и без сортировки отсортировался по цене (по возрастанию), как сделать так чтобы товар по умолчанию был отсортирован по дате добавления или обновления?
Добавлено через 6 минут
Также напишите пожалуйста кто знает как к этой сортировке добавить, варианты.
Мне надо: по цене, по дате добавления или обновления, по популярности (количество просмотров товара).
Также можно ли реализовать сортировку по вариантам товара (в моем случае это размеры одежды (42, 44, 46...))?