-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualProductDelivery.php
105 lines (88 loc) · 3.06 KB
/
VirtualProductDelivery.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
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace VirtualProductDelivery;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Model\Country;
use Thelia\Model\LangQuery;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Model\State;
use Thelia\Module\AbstractDeliveryModuleWithState;
use Thelia\Module\Exception\DeliveryException;
class VirtualProductDelivery extends AbstractDeliveryModuleWithState
{
public const MESSAGE_DOMAIN = 'virtualproductdelivery';
/** @var Translator */
protected $translator;
/**
* The module is valid if the cart contains only virtual products.
*
* @throws \Propel\Runtime\Exception\PropelException
*
* @return bool true if there is only virtual products in cart elsewhere false
*/
public function isValidDelivery(Country $country, State $state = null)
{
return $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->isVirtual();
}
public function getPostage(Country $country, State $state = null)
{
if (!$this->isValidDelivery($country, $state)) {
throw new DeliveryException(
$this->trans('This module cannot be used on the current cart.')
);
}
return 0.0;
}
/**
* This module manages virtual product delivery.
*
* @return bool
*/
public function handleVirtualProductDelivery()
{
return true;
}
public function postActivation(ConnectionInterface $con = null): void
{
// create new message
if (null === MessageQuery::create()->findOneByName('mail_virtualproduct')) {
$message = new Message();
$message
->setName('mail_virtualproduct')
->setHtmlTemplateFileName('virtual-product-download.html')
->setHtmlLayoutFileName('')
->setTextTemplateFileName('virtual-product-download.txt')
->setTextLayoutFileName('')
->setSecured(0);
$languages = LangQuery::create()->find();
foreach ($languages as $language) {
$locale = $language->getLocale();
$message->setLocale($locale);
$message->setSubject(
$this->trans('Order {$order_ref} validated. Download your files.', [], $locale)
);
$message->setTitle(
$this->trans('Virtual product download message', [], $locale)
);
}
$message->save();
}
}
protected function trans($id, $parameters = [], $locale = null)
{
if (null === $this->translator) {
$this->translator = Translator::getInstance();
}
return $this->translator->trans($id, $parameters, self::MESSAGE_DOMAIN, $locale);
}
}