Completed
Pull Request — master (#358)
by Simon
06:06 queued 01:52
created

Voucher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 9.4285
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace ShopwarePlugins\Connect\Subscribers;
9
10
use Enlight\Event\SubscriberInterface;
11
use ShopwarePlugins\Connect\Components\BasketHelper;
12
use ShopwarePlugins\Connect\Components\Helper;
13
14
/**
15
 * Handle vouchers, remove discounts and don't allow percentaged vouchers for connect baskets
16
 */
17
class Voucher implements SubscriberInterface
18
{
19
    /**
20
     * @var Helper
21
     */
22
    private $helper;
23
24
    /**
25
     * @var BasketHelper
26
     */
27
    private $basketHelper;
28
29
    /**
30
     * @var \Shopware_Components_Snippet_Manager
31
     */
32
    private $snippetManager;
33
34
    /**
35
     * @param Helper $helper
36
     * @param BasketHelper $basketHelper
37
     * @param \Shopware_Components_Snippet_Manager $snippetManager
38
     */
39
    public function __construct(Helper $helper, BasketHelper $basketHelper, \Shopware_Components_Snippet_Manager $snippetManager)
40
    {
41
        $this->helper = $helper;
42
        $this->basketHelper = $basketHelper;
43
        $this->snippetManager = $snippetManager;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public static function getSubscribedEvents()
50
    {
51
        return [
52
            'Shopware_Modules_Basket_AddVoucher_Start' => 'preventPercentagedVoucher',
53
            'Shopware_Modules_Basket_GetBasket_FilterSQL' => 'removeDiscount'
54
        ];
55
    }
56
57
    /**
58
     * Helper method to remove percentaged discounts from the basket if connect products are available
59
     *
60
     * Alternative:
61
     *  * Calculate discount only for the default products
62
     *  * Removed percentaged discounts only if connect product has a fixedPrice
63
     *
64
     * @event Shopware_Modules_Basket_GetBasket_FilterSQL
65
     */
66
    public function removeDiscount(\Enlight_Event_EventArgs $args)
67
    {
68
        $message = $this->snippetManager->getNamespace('frontend/connect/checkout')->get(
69
            'noPercentagedDiscountsAllowed',
70
            'In Kombination mit connect-Produkten sind keine prozentualen Rabatte möglich.',
71
            true
72
        );
73
74
        if ($this->helper->hasBasketConnectProducts(Shopware()->SessionID())) {
75
            $stmt = Shopware()->Db()->query(
76
                'DELETE FROM s_order_basket WHERE sessionID=? AND modus=3',
77
                [Shopware()->SessionID()]
78
            );
79
80
            // If rows where actually affected, show the corresponding message
81
            if ($stmt->rowCount()) {
82
                Shopware()->Template()->assign('sVoucherError', $message);
83
            }
84
        }
85
    }
86
87
    /**
88
     * Will not allow percentaged vouchers if connect products are in the basket
89
     *
90
     * @event Shopware_Modules_Basket_AddVoucher_Start
91
     *
92
     * @param \Enlight_Event_EventArgs $args
93
     * @return bool|null
94
     */
95
    public function preventPercentagedVoucher(\Enlight_Event_EventArgs $args)
96
    {
97
        $code = $args->getCode();
98
99
        if (!$this->helper->hasBasketConnectProducts(Shopware()->SessionID())) {
100
            return null;
101
        }
102
103
        $message = $this->snippetManager->getNamespace('frontend/connect/checkout')->get(
104
            'noPercentagedVoucherAllowed',
105
            'In Kombination mit connect-Produkten sind keine prozentualen Gutscheine möglich.',
106
            true
107
        );
108
109
        // Exclude general percentaged vouchers
110
        $result = $this->basketHelper->findPercentagedVouchers($code);
111
        if (!empty($result)) {
112
            Shopware()->Template()->assign('sVoucherError', $message);
113
114
            return true;
115
        }
116
117
        // Exclude $this->basketHelper percentaged vouchers
118
        $result = $this->basketHelper->findPercentagedIndividualVouchers($code);
119
        if (!empty($result)) {
120
            Shopware()->Template()->assign('sVoucherError', $message);
121
122
            return true;
123
        }
124
    }
125
}
126