How to Add Sorting by Date to the Product List in OpenCart 2.x

Quick and easy products management is vital when organizing your store. To help you with that, OpenCart features filtering and sort ordering of your products. By default, the list of products is ordered by product ID. Additionally, you can order it by product name, model, price, quantity and status. 

In this blogpost, we will demonstrate how you can easily add a new field to your products list - date of creation and a date sorting. This will enable you to order your products starting from the most recent ones to the oldest ones or vice versa. 

What you have to do is to open your favorite text editor (DreamWeaver, Sublime, Notepad, etc. ) and copy and paste the following code:

<modification>
    <name>Filter Products by iSenseLabs</name>
    <version>1.0</version>
    <link>https://isenselabs.com</link>
    <author>iSenseLabs</author>
    <code>isense_filter_products_by_date</code>

    <file path="admin/controller/catalog/product.php">
        <operation>
            <search><![CDATA[protected function getList() {]]></search>
            <add position="after"><![CDATA[if (isset($this->request->get['filter_date_added'])) {
                  $filter_date = $this->request->get['filter_date_added'];
                      } else {
                            $filter_date = null;
                      }
             ]]></add>
        </operation>
        <operation>
            <search><![CDATA['filter_status'   => $filter_status,]]></search>
            <add position="after"><![CDATA['filter_date'     => $filter_date,
           ]]></add>
        </operation>
        <operation>
            <search><![CDATA['status'     => ($result['status']) ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),]]></search>
            <add position="after"><![CDATA[
                  'date_added' => $result['date_added'],
             ]]></add>
        </operation>
        <operation>
            <search><![CDATA[ $this->data['breadcrumbs'] = array();]]></search>
            <add position="before" index="1"><![CDATA[                        
                    if (isset($this->request->get['filter_date_added'])) {
                        $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
                    }
                ]]></add>
        </operation>
        <operation>
            <search><![CDATA[$data['breadcrumbs'] = array();]]></search>
            <add position="before" index="1"><![CDATA[                        
                    if (isset($this->request->get['filter_date_added'])) {
                        $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
                    }
                ]]></add>
        </operation>   
    </file>    

    <file path="admin/model/catalog/product.php">
        <operation>
            <search><![CDATA[$sql = "SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";]]></search>
            <add position="after"><![CDATA[if (isset($data['filter_date_added']) && !is_null($data['filter_date_added'])) {
                      $sql .= " ORDER BY p.date_added '" . (int)$data['filter_date_added'] . "'";
                     }
               ]]></add>
        </operation>
        <operation>
            <search><![CDATA['p.sort_order']]></search>
            <add position="before"><![CDATA[
                'p.date_added',
             ]]></add>
        </operation>
    </file>

    <file path="admin/view/template/catalog/product_list.tpl">
        <operation>
            <search><![CDATA[<td class="text-right"><?php echo $column_action; ?></td>]]></search>
            <add position="before"><![CDATA[
                <td class="text-left">
                    <a href="<?php echo $sort_status.'&sort=p.date_added'; ?>">Date</a>
                </td>
            ]]></add>
        </operation>
        <operation>
            <search><![CDATA[<td class="text-right"><a href="<?php echo $product['edit']; ?>" data-toggle="tooltip" title="<?php echo $button_edit; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a></td>]]></search>
            <add position="before"><![CDATA[ 
                    <td class="text-left"><?php echo $product['date_added']; ?></td>
              ]]></add>
        </operation>
        <operation>
            <search><![CDATA[<button type="button" id="button-filter" class="btn btn-primary pull-right"><i class="fa fa-search"></i> <?php echo $button_filter; ?></button>]]></search>
            <add position="before"><![CDATA[<td class="center">&nbsp</td>]]></add>
        </operation>
    </file>

    <file path="admin/view/template/catalog/product_list.tpl">
        <operation>
            <search><![CDATA[<button type="button" id="button-filter" class="btn btn-primary pull-right"><i class="fa fa-filter"></i> <?php echo $button_filter; ?>]]></search>
            <add position="before"><![CDATA[<td class="center">&nbsp</td>]]></add>
        </operation>
    </file>
</modification>


Don’t forget to save the file with the extension .ocmod.xml and upload it through the extension installer (Admin Panel > Extensions > Extension Installer). Refresh the modifications and test the feature by going to Catalog > Products in your admin panel. If the modifications have been applied correctly, you should get the following result:

‚Äč

By clicking on the Date field, your products will be reordered.

We hope that this fast and easy tutorial will eventually come in handy when managing your OpenCart products. 

Join 11,000+ subscribers receiving actionable E-commerce advice

* Unsubscribe any time

Trending blogs

comments powered by Disqus