Skip to content

Commit

Permalink
Added filter by Manufacturer in Product List
Browse files Browse the repository at this point in the history
  • Loading branch information
condor2 committed Aug 19, 2020
1 parent 1ad838b commit f3b15c5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -29,6 +29,7 @@ This is a modified version of Opencart 3.0.3.6.
- Show/Hide password. Code used from <a href="https://github.com/opencartbrasil/opencartbrasil">Opencart Brasil</a>
- Manufacturer Layout Override
- {% include %} function support
- Filter by Manufacturer in Product List

## Note
This version is not supported by Opencart. You need to have at least PHP 7.3 to use this version.
Expand Down
22 changes: 22 additions & 0 deletions upload/admin/controller/catalog/product.php
Expand Up @@ -231,6 +231,12 @@ protected function getList() {
$filter_name = '';
}

if (isset($this->request->get['filter_manufacturer'])) {
$filter_manufacturer = $this->request->get['filter_manufacturer'];
} else {
$filter_manufacturer = '';
}

if (isset($this->request->get['filter_model'])) {
$filter_model = $this->request->get['filter_model'];
} else {
Expand Down Expand Up @@ -285,6 +291,10 @@ protected function getList() {
$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
}

if (isset($this->request->get['filter_manufacturer'])) {
$url .= '&filter_manufacturer=' . $this->request->get['filter_manufacturer'];
}

if (isset($this->request->get['filter_model'])) {
$url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
}
Expand Down Expand Up @@ -333,6 +343,7 @@ protected function getList() {

$filter_data = array(
'filter_name' => $filter_name,
'filter_manufacturer' => $filter_manufacturer,
'filter_model' => $filter_model,
'filter_price' => $filter_price,
'filter_quantity' => $filter_quantity,
Expand All @@ -350,6 +361,16 @@ protected function getList() {

$results = $this->model_catalog_product->getProducts($filter_data);

$this->load->model('catalog/manufacturer');

$data['manufacturers'][] = array(
'manufacturer_id' => '0',
'name' => $this->language->get('entry_no_manufacturers'),
'sort_order' => '0'
);

$data['manufacturers'] = array_merge($data['manufacturers'], $this->model_catalog_manufacturer->getManufacturers($filter_data));

foreach ($results as $result) {
if (is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
$image = $this->model_tool_image->resize(html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'), 40, 40);
Expand Down Expand Up @@ -493,6 +514,7 @@ protected function getList() {
$data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($product_total - $this->config->get('config_limit_admin'))) ? $product_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $product_total, ceil($product_total / $this->config->get('config_limit_admin')));

$data['filter_name'] = $filter_name;
$data['filter_manufacturer'] = $filter_manufacturer;
$data['filter_model'] = $filter_model;
$data['filter_price'] = $filter_price;
$data['filter_quantity'] = $filter_quantity;
Expand Down
1 change: 1 addition & 0 deletions upload/admin/language/en-gb/catalog/product.php
Expand Up @@ -29,6 +29,7 @@

// Entry
$_['entry_name'] = 'Product Name';
$_['entry_no_manufacturers'] = '- None -';
$_['entry_description'] = 'Description';
$_['entry_meta_title'] = 'Meta Tag Title';
$_['entry_meta_keyword'] = 'Meta Tag Keywords';
Expand Down
6 changes: 6 additions & 0 deletions upload/admin/model/catalog/product.php
Expand Up @@ -362,6 +362,12 @@ public function getProducts($data = array()) {
$sql .= " AND pd.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
}

if (isset($data['filter_manufacturer'])) {
if (!empty($data['filter_manufacturer']) || $data['filter_manufacturer'] == '0') {
$sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer'] . "'";
}
}

if (!empty($data['filter_model'])) {
$sql .= " AND p.model LIKE '" . $this->db->escape($data['filter_model']) . "%'";
}
Expand Down
19 changes: 19 additions & 0 deletions upload/admin/view/template/catalog/product_list.twig
Expand Up @@ -37,6 +37,19 @@
<label class="control-label" for="input-name">{{ entry_name }}</label>
<input type="text" name="filter_name" value="{{ filter_name }}" placeholder="{{ entry_name }}" id="input-name" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="input-manufacturer">{{ entry_manufacturer }}</label>
<select name="filter_manufacturer" id="input-manufacturer" class="form-control">
<option value=""></option>
{% for manufacturer in manufacturers %}
{% if manufacturer.manufacturer_id == filter_manufacturer %}
<option value="{{ manufacturer.manufacturer_id }}" selected="selected">{{ manufacturer.name }}&nbsp;&nbsp;&nbsp;&nbsp;</option>
{% else %}
<option value="{{ manufacturer.manufacturer_id }}">&nbsp;&nbsp;{{ manufacturer.name }}&nbsp;&nbsp;&nbsp;&nbsp;</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="form-group">
<label class="control-label" for="input-model">{{ entry_model }}</label>
<input type="text" name="filter_model" value="{{ filter_model }}" placeholder="{{ entry_model }}" id="input-model" class="form-control" />
Expand Down Expand Up @@ -160,6 +173,12 @@ $('#button-filter').on('click', function() {
url += '&filter_name=' + encodeURIComponent(filter_name);
}
var filter_manufacturer = $('select[name=\'filter_manufacturer\']').val();
if (filter_manufacturer != '*') {
url += '&filter_manufacturer=' + encodeURIComponent(filter_manufacturer);
}
var filter_model = $('input[name=\'filter_model\']').val();
if (filter_model) {
Expand Down

0 comments on commit f3b15c5

Please sign in to comment.