improve photos

This commit is contained in:
O K
2025-11-25 11:00:26 +02:00
parent ff2dcdc0ee
commit 8e7141175a
6 changed files with 1004 additions and 719 deletions

View File

@@ -84,16 +84,14 @@ class AddLivePhoto extends Module
$this->uninstallAdminTab();
}
/**
* Create the database table for storing image information.
* @return bool
*/
protected function installDb()
protected function installDb()
{
// Added image_type column
$sql = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . self::TABLE_NAME . '` (
`id_add_live_photo` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_product` INT(11) UNSIGNED NOT NULL,
`image_name` VARCHAR(255) NOT NULL,
`image_type` ENUM("expiry", "packaging") NOT NULL DEFAULT "expiry",
`date_add` DATETIME NOT NULL,
PRIMARY KEY (`id_add_live_photo`),
INDEX `id_product_idx` (`id_product`)
@@ -177,48 +175,51 @@ class AddLivePhoto extends Module
}
$id_product = (int) Tools::getValue('id_product');
if (!$id_product) {
return;
}
if (!$id_product) return;
// Fetch images from the last 4 months
$sql = new DbQuery();
$sql->select('`image_name`');
$sql->from(self::TABLE_NAME);
$sql->where('`id_product` = ' . $id_product);
$sql->where('`date_add` >= DATE_SUB(NOW(), INTERVAL 4 MONTH)');
$sql->orderBy('`date_add` DESC');
// Complex Logic:
// 1. Get 'packaging' photos (Always show, limit to newest 3)
// 2. Get 'expiry' photos (Show only if newer than 3 months)
$sql = "SELECT * FROM `" . _DB_PREFIX_ . self::TABLE_NAME . "`
WHERE `id_product` = " . $id_product . "
AND (
(`image_type` = 'packaging')
OR
(`image_type` = 'expiry' AND `date_add` >= DATE_SUB(NOW(), INTERVAL 3 MONTH))
)
ORDER BY `date_add` DESC";
$results = Db::getInstance()->executeS($sql);
if (!$results) {
return;
}
if (!$results) return;
$live_photos = [];
foreach ($results as $row) {
$image_uri = $this->getProductImageUri($id_product, $row['image_name']);
if ($image_uri) {
// Customize text based on type
$is_expiry = ($row['image_type'] === 'expiry');
$date_taken = date('Y-m-d', strtotime($row['date_add']));
$alt_text = $is_expiry
? sprintf($this->trans('Expiry date photo for %s, taken on %s',[], 'Modules.Addlivephoto.Shop'), $this->context->smarty->tpl_vars['product']->value['name'], $date_taken)
: sprintf($this->trans('Real packaging photo for %s',[], 'Modules.Addlivephoto.Shop'), $this->context->smarty->tpl_vars['product']->value['name']);
$live_photos[] = [
'url' => $image_uri,
// This alt text is crucial for SEO
'alt' => sprintf(
$this->trans('Freshness photo for %s, taken on %s',[], 'Modules.Addlivephoto.Shop'),
$this->context->smarty->tpl_vars['product']->value['name'],
date('Y-m-d') // You can store the date_add and format it here
),
'title' => $this->trans('Click to see the expiry date photo',[], 'Modules.Addlivephoto.Shop'),
'type' => $row['image_type'], // 'expiry' or 'packaging'
'date' => $row['date_add'],
'alt' => $alt_text,
];
}
}
if (empty($live_photos)) {
return;
}
if (empty($live_photos)) return;
$this->context->smarty->assign([
'live_photos' => $live_photos,
'module_name' => $this->name,
]);
return $this->display(__FILE__, 'views/templates/hook/displayProductPriceBlock.tpl');
@@ -231,8 +232,8 @@ class AddLivePhoto extends Module
{
// We only want to load these assets on our specific controller page
if (Tools::getValue('controller') == 'AdminAddLivePhoto') {
$this->context->controller->addJS($this->_path . 'views/js/admin.js');
$this->context->controller->addCSS($this->_path . 'views/css/admin.css');
// $this->context->controller->addJS($this->_path . 'views/js/admin.js');
// $this->context->controller->addCSS($this->_path . 'views/css/admin.css');
}
}