R
R
Roman Buns2015-08-08 20:09:04
opencart
Roman Buns, 2015-08-08 20:09:04

How to sort by discount size in opencart?

The /specials page displays all discounted products. It is necessary that the products with the highest discount are displayed first. Or to display products with a discount greater than a certain one.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Evgrafovich, 2015-08-10
@Tantacula

Look for the controller of the module responsible for this, it should have settings, or at least it will be written in the code where it takes the settings from (). In the standard Opencart1564, there is no such path by default, there is only a module for displaying on other pages, it is located in catalog/controller/module/special.php, if your specials page was written not quite by a crooked specialist, then he should have used the same controller. Sorting there is set by the fields:

'sort'  => 'pd.name',
'order' => 'ASC',

You can either write filtering above a certain price yourself (it’s not difficult in theory), or freelance if you don’t own php.

H
harlamov_di, 2019-07-26
@harlamov_di

The easiest way for stocks, at least which seemed adequate to me when solving this problem and helped me:
1. Create a column in the oc_product table and name it, for example, product_special, type INT (10)
2. In the file admin/model/catalog/product we find two functions addProduct and editProduct
3. In each of them, in the body of the function, we find a check for the existence of a stock in the product and write the following query to the oc_product table:
$this->db->query("UPDATE " . DB_PREFIX . "product SET product_special = '" . (float)$product_special['price'] . "' WHERE product_id = '" . (int)$product_id . "'");
Here is how the condition itself looks like and already the query that needs to be inserted there:

if (isset($data['product_special'])) {
      foreach ($data['product_special'] as $product_special) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "product_special SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$product_special['customer_group_id'] . "', priority = '" . (int)$product_special['priority'] . "', price = '" . (float)$product_special['price'] . "', date_start = '" . $this->db->escape($product_special['date_start']) . "', date_end = '" . $this->db->escape($product_special['date_end']) . "'");
      }
            
            $this->db->query("UPDATE " . DB_PREFIX . "product SET product_special = '" . (float)$product_special['price'] . "' WHERE product_id = '" . (int)$product_id . "'");
    }

Now, when adding a product or editing it to the oc_product table, the price of the promotional product will be entered in the -product_special column.
4. In the /catalog/controller/category/category.php file, add a new sorting to $this->language->get('we write whatever is the text of the sort itself'), set ASC if we need ascending, DESC if descending :
$data['sorts'][] = array(
        'text'  => $this->language->get('text_special_asc'),
        'value' => 'p.product_special-asc',
        'href'  => $this->url->link($route, $path . '&sort=p.product_special&order=asc' . $url)
      );

5. In the file /catalog/model/product.php
In the getProducts function we find the line:
$sort_data = array(
      'pd.name',
      'p.model',
      'p.quantity',
      'p.price',
и тд...
    );

and add a new line p.product_special-asc there and get:
$sort_data = array(
      'pd.name',
      'p.model',
      'p.quantity',
      'p.price',
      'rating',
      'p.sort_order',
      'p.date_added',
                       'p.product_special-asc'
    );

that's all basically

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question