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
|
<?php
namespace Jeasy\Common\Model\Checkout;
class NavsManagement implements \Jeasy\Common\Api\Checkout\NavsManagementInterface
{
protected $scopeConfig;
protected $priceHelper;
protected $shippingHelper;
protected $session;
protected $objectManager;
protected $dataFactory;
protected $extraDiscountRules;
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\Pricing\Helper\Data $priceHelper,
\Jeasy\Shipping\Helper\Data $shippingHelper,
\Magento\Checkout\Model\Session $session,
\Magento\Framework\ObjectManagerInterface $objectManager,
\Jeasy\Common\Api\Data\Checkout\NavsInterfaceFactory $dataFactory
) {
$this->scopeConfig = $scopeConfig;
$this->priceHelper = $priceHelper;
$this->shippingHelper = $shippingHelper;
$this->session = $session;
$this->objectManager = $objectManager;
$this->dataFactory = $dataFactory;
return $this;
}
public function getList()
{
/** @var \Jeasy\Common\Api\Data\Checkout\NavsInterface $res */
$res = $this->dataFactory->create();
$res->setShippingTip($this->getFreeShippingTip())
->setDiscountTip($this->getDiscountTip());
return $res;
}
protected function getQuote()
{
return $this->session->getQuote();
}
public function getFreeShippingTip()
{
$quote = $this->getQuote();
$countryId = $quote->getShippingAddress()->getCountryId();
$setting = $this->shippingHelper->loadFreeShippingSetting($countryId);
$allowMethods = []; // allow free shipping methods
foreach ($setting as $methodCode => $item) {
if ($item['free_shipping_over'] && $item['apply_free_shipping']) {
$allowMethods[] = $methodCode;
}
}
$freeShippingOver = [
'tablerate_bestway' => [
'value' => $this->scopeConfig->getValue('carriers/tablerate/free_shipping_over'),
'name' => __(''),
],
'accelerate_standard' => [
'value' => $this->scopeConfig->getValue('carriers/accelerate/free_shipping_over'),
'name' => __('STANDARD'),
],
'expedite_expedite' => [
'value' => $this->scopeConfig->getValue('carriers/expedite/free_shipping_over'),
'name' => __('EXPRESS'),
],
];
$baseSpread = 0;
$name = '';
$baseSubtotalWithDiscount = (float) $quote->getBaseSubtotalWithDiscount();
foreach ($freeShippingOver as $key => $method) {
if (!in_array($key, $allowMethods)) {
continue;
}
$value = (float) $method['value'];
if ($baseSubtotalWithDiscount < $value) {
$baseSpread = $value - $baseSubtotalWithDiscount;
$name = __($method['name']);
break;
}
}
if ($baseSpread > 0.01) {
$spread = $this->priceHelper->currency($baseSpread, true, false);
return sprintf(__('Spend <span class="price">%s</span> more to enjoy FREE %s SHIPPING'), $spread, $name);
}
return '';
}
public function getDiscountTip()
{
$quote = $this->getQuote();
$baseSubtotal = (float) $quote->getBaseSubtotal();
$nextDiscountPercentage = 0;
$baseSpread = 0;
$currentPercentage = 0;
$extraDiscountRules = $this->_getExtraDiscountRules();
foreach ($extraDiscountRules as $key => $extraDiscountRule) {
if ($extraDiscountRule['low_price'] <= $baseSubtotal) {
$currentPercentage = $extraDiscountRule['discount_percentage'];
$highPrice = $extraDiscountRule['high_price'];
if ($highPrice > 0.01 && $highPrice > $baseSubtotal) {
$baseSpread = $highPrice - $baseSubtotal;
}
if (isset($extraDiscountRules[$key + 1])) {
$nextDiscountPercentage = $extraDiscountRules[$key + 1]['discount_percentage'];
}
}
}
$tip = '';
if ($currentPercentage) {
$tip .= sprintf(__('You have already enjoyed EXTRA %s%% discount. '), $currentPercentage);
}
if ($baseSpread && $nextDiscountPercentage) {
$spread = $this->priceHelper->currency($baseSpread, true, false);
$tip .= sprintf(__(' Spend <span class="price">%s</span> more to enjoy Extra %s%% discount.'), $spread, $nextDiscountPercentage);
}
return $tip;
}
protected function _getExtraDiscountRules()
{
if ($this->extraDiscountRules === null) {
$rules = explode(';', $this->scopeConfig->getValue('promotion/extra_discount/rule'));
$this->extraDiscountRules = [];
if (count($rules)) {
foreach ($rules as $rule) {
$_rule = explode('|', $rule);
if (count($_rule) === 2) {
$priceRange = explode('~', $_rule[0]);
$discountPercentage = $_rule[1];
if (count($priceRange) === 2 && $discountPercentage > 0) {
$this->extraDiscountRules[] = [
'low_price' => (float) $priceRange[0],
'high_price'=> (float) $priceRange[1],
'discount_percentage' => (int) $discountPercentage,
];
}
}
}
}
}
return $this->extraDiscountRules;
}
}
|