-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnnwebVoucherLink.php
171 lines (137 loc) · 6.29 KB
/
nnwebVoucherLink.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace nnwebVoucherLink;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UpdateContext;
class nnwebVoucherLink extends \Shopware\Components\Plugin {
private $config;
public static function getSubscribedEvents() {
return [
'Enlight_Controller_Action_PreDispatch_Frontend_Checkout' => 'onPreDispatchCheckout',
'Enlight_Controller_Action_PostDispatchSecure' => 'onPostDispatch',
'Enlight_Controller_Dispatcher_ControllerPath_Frontend_Gutschein' => 'getFrontendVoucherLinkController'
];
}
public function activate(ActivateContext $context) {
$context->scheduleClearCache(InstallContext::CACHE_LIST_DEFAULT);
parent::activate($context);
}
public function deactivate(DeactivateContext $context) {
$context->scheduleClearCache(InstallContext::CACHE_LIST_DEFAULT);
parent::deactivate($context);
}
public function update(UpdateContext $context) {
$context->scheduleClearCache(InstallContext::CACHE_LIST_DEFAULT);
parent::update($context);
}
public function getFrontendVoucherLinkController() {
return $this->getPath() . '/Controllers/Frontend/VoucherLink.php';
}
public function onPostDispatch(\Enlight_Controller_ActionEventArgs $args) {
$this->config = $this->container->get('shopware.plugin.cached_config_reader')->getByPluginName($this->getName());
$controller = $args->getSubject();
$request = $controller->Request();
$response = $controller->Response();
$view = $controller->View();
if (!$request->isDispatched() || $response->isException() || !$view->hasTemplate() || $request->getModuleName() != 'frontend') {
return;
}
if (!empty(Shopware()->Session()->nnwebVoucherLinkFromController)) {
$voucherCode = Shopware()->Session()->nnwebVoucherLinkCode;
Shopware()->Session()->nnwebVoucherLinkFromController = false;
} else {
$voucherCode = $request->getParam('gutschein');
}
$this->addVoucherCode($view, $voucherCode);
// Falls ich über den Controller gekommen bin
if (($request->getControllerName() == 'index' && $request->getActionName() == 'index')) {
$message = Shopware()->Session()->nnwebVoucherLinkMessage;
$type = Shopware()->Session()->nnwebVoucherLinkMessageType;
if (!empty($message)) {
$view->assign('nnwebVoucherLinkMessage', $message);
$view->assign('nnwebVoucherLinkMessageType', $type);
Shopware()->Session()->nnwebVoucherLinkMessage = '';
Shopware()->Session()->nnwebVoucherLinkMessageType = '';
}
}
$this->container->get('template')->addTemplateDir($this->getPath() . '/Resources/views/');
}
public function onPreDispatchCheckout(\Enlight_Controller_ActionEventArgs $args) {
$this->config = $this->container->get('shopware.plugin.cached_config_reader')->getByPluginName($this->getName());
$controller = $args->getSubject();
$request = $controller->Request();
$view = $controller->View();
if ($request->getActionName() != 'cart' && $request->getActionName() != 'confirm') {
return;
}
$voucherCode = Shopware()->Session()->nnwebVoucherLinkCode;
$this->addVoucherCode($view, $voucherCode, true);
}
private function addVoucherCode($view, $voucherCode, $isCart = false) {
$snippets = Shopware()->Container()->get('snippets')->getNamespace('frontend/plugins/nnwebVoucherLink');
if (!empty($voucherCode)) {
$basket = Shopware()->Modules()->Basket();
$voucher = $basket->sAddVoucher($voucherCode);
if ($voucher === false) {
$message = $snippets->get('voucher', 'Der Gutscheincode');
$message .= ' <b>' . $voucherCode . '</b> ';
if (!empty($this->config["nnwebVoucherLink_voucherInfo"])) {
$voucherInfo = $this->getVoucherInfo($voucherCode);
if (!empty($voucherInfo))
$message .= '(' . $this->getVoucherInfo($voucherCode) . ') ';
}
$message .= $snippets->get('saved', 'wurde gespeichert und wird automatisch in den Warenkorb gelegt!');
$type = 'success';
Shopware()->Session()->nnwebVoucherLinkCode = $voucherCode;
} elseif ($voucher['sErrorFlag'] == 1) {
$message = implode(', ', $voucher['sErrorMessages']);
Shopware()->Session()->nnwebVoucherLinkCode = $voucherCode;
$type = 'warning';
} else {
$message = $snippets->get('voucher', 'Der Gutscheincode');
$message .= ' <b>' . $voucherCode . '</b> ';
if (!empty($this->config["nnwebVoucherLink_voucherInfo"])) {
$voucherInfo = $this->getVoucherInfo($voucherCode);
if (!empty($voucherInfo))
$message .= '(' . $this->getVoucherInfo($voucherCode) . ') ';
}
if ($isCart) {
$message .= $snippets->get('redeemed', 'wurde erfolgreich eingelöst!');
} else {
$message .= $snippets->get('saved', 'wurde gespeichert und wird automatisch in den Warenkorb gelegt!');
}
Shopware()->Session()->nnwebVoucherLinkCode = "";
$type = 'success';
}
}
$view->assign('nnwebVoucherLinkMessage', $message);
$view->assign('nnwebVoucherLinkMessageType', $type);
}
private function getVoucherInfo($voucherCode) {
$voucherDetails = Shopware()->Db()->fetchRow('SELECT sev.*, sas.name as supplierName
FROM s_emarketing_vouchers sev
LEFT JOIN s_emarketing_voucher_codes sevc ON sev.id = sevc.voucherID
LEFT JOIN s_articles_supplier sas ON sev.bindtosupplier = sas.id
WHERE (sev.vouchercode = ? OR sevc.code = ?)
AND (
(sev.valid_to >= CURDATE() AND sev.valid_from <= CURDATE())
OR sev.valid_to IS NULL
)', [strtolower($voucherCode), strtolower($voucherCode)]) ?: [];
$voucherInfo = "";
if (!empty($voucherDetails)) {
if (empty($voucherDetails["percental"])) {
$voucherInfo .= $voucherDetails["value"] . "€";
} else {
$voucherInfo .= $voucherDetails["value"] . "%";
}
if (!empty($voucherDetails["minimumcharge"])) {
$voucherInfo .= " ab " . $voucherDetails["minimumcharge"] . "€ Bestellwert";
}
if (!empty($voucherDetails["supplierName"])) {
$voucherInfo .= " auf " . $voucherDetails["supplierName"] . " Produkte";
}
}
return $voucherInfo;
}
}