-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailstock.php
87 lines (75 loc) · 2.54 KB
/
mailstock.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
if (!defined('_PS_VERSION_'))
exit;
class MailStock extends Module
{
public function __construct($name = null, Context $context = null)
{
$this->name = 'mailstock';
$this->tab = 'others';
$this->version = '1.0';
$this->author = 'Vivien Marnier';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct($name, $context);
$this->displayName = $this->l('MailStock');
$this->description = $this->l('A simple module to send emails when a product stock is updated.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall MailStock ?');
if (!Configuration::get('MYMODULE_NAME'))
$this->warning = $this->l('No name provided');
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install() ||
!$this->registerHook('actionUpdateQuantity') ||
!Configuration::updateValue('MYMODULE_NAME', 'MailStock')
)
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall() ||
!Configuration::deleteByName('MYMODULE_NAME')
)
return false;
return true;
}
public function hookActionUpdateQuantity($params)
{
if((isset($params['id_product'])) && (isset($params['quantity'])))
{
$this->sendMail($params['id_product'],$params['quantity']);
}
}
/**
* @param $productId
* @param $quantity
* setup and send an email to a specific recipient which notify the new quantity of specific product
*/
private function sendMail($productId,$quantity)
{
$mailSubject = 'Product quantity updated';
$productName = $this->getProductName($productId);
$template = "product_quantity_updated";
$recipient = "[email protected]";
$data = [
'quantity' => $quantity,
'productName' =>$productName,
];
Mail::Send($this->context->language->id,$template,$mailSubject,$data,$recipient,null,null,null,null,null,'mails/');
}
/**
* @param $productId
* @return string
* get the product name from the product id
*/
private function getProductName($productId)
{
$product = new Product($productId, false, $this->context->language->id);
return $product->name;
}
}