Post by anubhav_majumder » Sat Feb 11, 2023 10:22 pm

Hello everyone,

I am working on a product expiration module, meaning I can set a time/date for the product to expire automatically and no longer be available to purchase/download.

I created the module under: catalog/controller/module/product_expiry/product_expiry.php

Code: Select all

<?php

class ControllerModuleProductExpiry extends Controller {
    public function index($setting) {
        $this->load->language('module/product_expiry');

        $data['heading_title'] = $this->language->get('heading_title');

        if (isset($setting['title'][$this->config->get('config_language_id')])) {
            $data['heading_title'] = $setting['title'][$this->config->get('config_language_id')];
        }

        $this->load->model('catalog/product');
        $this->load->model('module/product_expiry');

        $products = $this->model_catalog_product->getProducts();

        foreach ($products as $product) {
            $product_id = $product['product_id'];
            $expiry_date = $this->model_module_product_expiry->getProductExpiry($product_id);

            if ($expiry_date) {
                $current_date = date("Y-m-d");

                if ($current_date > $expiry_date) {
                    $product['expired'] = true;
                } else {
                    $product['expired'] = false;
                }
            } else {
                $product['expired'] = false;
            }

            $data['products'][] = $product;
        }

        return $this->load->view('module/product_expiry', $data);
    }
}
Then i created a template file under catalog/view/template/module/product_expiry.tpl

Code: Select all

<div class="product-expiry">
  <h3><?php echo $heading_title; ?></h3>
  <table>
    <tr>
      <th><?php echo $column_product; ?></th>
      <th><?php echo $column_expiry; ?></th>
      <th><?php echo $column_status; ?></th>
    </tr>
    <?php foreach ($products as $product) { ?>
      <tr>
        <td><?php echo $product['name']; ?></td>
        <td><?php echo $product['expiry_date']; ?></td>
        <td>
          <?php if ($product['expired']) { ?>
            <span class="expired"><?php echo $text_expired; ?></span>
          <?php } else { ?>
            <span class="valid"><?php echo $text_valid; ?></span>
          <?php } ?>
        </td>
      </tr>
    <?php } ?>
  </table>
</div>
And a model file under catalog/model/module/product_expiry.php

Code: Select all

<?php

class ModelModuleProductExpiry extends Model {
    public function getProductExpiry($product_id) {
        $query = $this->db->query("SELECT expiry_date FROM " . DB_PREFIX . "product_expiry WHERE product_id = '" . (int)$product_id . "'");

        if ($query->num_rows) {
            return $query->row['expiry_date'];
        } else {
            return false;
        }
    }
}
And a table `oc_product_expiry' under opencart structure

And under admin/controller/extension/module.php I changed the
$installed = [];
to
$installed[] = 'product_expiry';

Any help is appreciated.


Posts

Joined
Sun Apr 07, 2019 10:32 pm

Post by JNeuhoff » Sun Feb 12, 2023 2:27 am

Maybe use a CRON-task to call up your index.php?route=extension/module/product_expiry every hour, or daily? No need for an elaborate twig-template for the catalog frontend for this module if only called by the CROM-task. Just a quick thought.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by anubhav_majumder » Sun Feb 12, 2023 12:41 pm

The problem is that this module is not showing under Extensions - Module.
I have checked System - Maintainance - Error Log. Nothing.

Kindly assist


Posts

Joined
Sun Apr 07, 2019 10:32 pm

User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by anubhav_majumder » Mon Feb 13, 2023 3:46 pm

Version 4.0.1.1


Posts

Joined
Sun Apr 07, 2019 10:32 pm

Post by JNeuhoff » Mon Feb 13, 2023 6:37 pm

anubhav_majumder wrote:
Mon Feb 13, 2023 3:46 pm
Version 4.0.1.1
No wonder it won't work. Extensions for OpenCart 4 are written up differently. See this documentation on how to do it.

Also be aware that none of the OpenCart 4.x releases are yet suitable for live sites, OpenCart 4 should only be used by developers at the moment.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by laur » Wed Feb 15, 2023 3:24 pm

JNeuhoff wrote:
Mon Feb 13, 2023 6:37 pm
anubhav_majumder wrote:
Mon Feb 13, 2023 3:46 pm
Version 4.0.1.1
No wonder it won't work. Extensions for OpenCart 4 are written up differently. See this documentation on how to do it.

Also be aware that none of the OpenCart 4.x releases are yet suitable for live sites, OpenCart 4 should only be used by developers at the moment.
OpenCart 4 is not marked as used on development purposes only. Since it's available to download as LTS then he should be fine using it on live website. Otherwise it wouldn't be set as default version to download on OpenCart download page.
Have a nice day!

Newbie

Posts

Joined
Fri Oct 31, 2014 7:35 am
Location - Bucharest

Post by IP_CAM » Wed Feb 15, 2023 7:01 pm

@laur: Well, you're probably just looking for a Job, to assist those, trying to get OC v.4.x to work, :laugh: But with only 2 Postings during 8+ Years here, except of those 5 of today, you just don't seem to know, how the system here works. No Offense, just figured ... :D

BTW: The official OC Release is out of date anyway:
define('VERSION', '4.0.1.1');
https://www.opencart.com/index.php?route=cms/download

And the latest Github Release is already:
define('VERSION', '4.0.2.0');
https://github.com/opencart/opencart/bl ... /index.php

My Github OC Site: https://github.com/IP-CAM
5'600 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland
Who is online

Users browsing this forum: No registered users and 2 guests