-
Notifications
You must be signed in to change notification settings - Fork 1
/
kptopsellerslabel.php
66 lines (55 loc) · 1.87 KB
/
kptopsellerslabel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
class KpTopSellersLabel extends Module
{
public static $topSellersIds = [];
public function __construct()
{
$this->name = 'kptopsellerslabel';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->ps_versions_compliancy = array(
'min' => '1.7.6.0',
'max' => _PS_VERSION_
);
$this->author = 'Krystian Podemski';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Top seller label', array(), 'Modules.Kptopsellerslabel.Admin');
$this->description = $this->trans('Module will show "top seller" label on product list.', array(), 'Modules.Kptopsellerslabel.Admin');
}
public function isUsingNewTranslationSystem()
{
return true;
}
public function install()
{
return parent::install() && $this->registerHook('actionProductFlagsModifier');
}
public function getTopSellers()
{
$topsellers = Db::getInstance()->ExecuteS('
SELECT * FROM `'._DB_PREFIX_.'product_sale`
ORDER BY quantity DESC, date_upd DESC
LIMIT 3
');
$topSellersIds = array_map(function($product) {
return $product['id_product'];
}, $topsellers);
return $topSellersIds;
}
public function hookActionProductFlagsModifier($params)
{
$flags = $params['flags'];
$product = $params['product'];
if (!count(self::$topSellersIds)) {
self::$topSellersIds = $this->getTopSellers();
}
if (in_array($product['id_product'], self::$topSellersIds)) {
$flags['topseller'] = [
'type' => 'topseller',
'label' => $this->trans('Top seller', array(), 'Modules.Kptopsellerslabel.Admin')
];
}
$params['flags'] = $flags;
}
}