5
5
*/
6
6
namespace Magento \Bundle \Helper \Catalog \Product ;
7
7
8
+ use Magento \Bundle \Model \Product \Price ;
9
+ use Magento \Bundle \Model \Product \Type ;
10
+ use Magento \Bundle \Pricing \Price \TaxPrice ;
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
12
+ use Magento \Catalog \Helper \Product \Configuration as ProductConfiguration ;
8
13
use Magento \Catalog \Helper \Product \Configuration \ConfigurationInterface ;
14
+ use Magento \Catalog \Model \Product ;
9
15
use Magento \Catalog \Model \Product \Configuration \Item \ItemInterface ;
10
16
use Magento \Framework \App \Helper \AbstractHelper ;
17
+ use Magento \Framework \App \Helper \Context ;
18
+ use Magento \Framework \App \ObjectManager ;
19
+ use Magento \Framework \Escaper ;
20
+ use Magento \Framework \Exception \LocalizedException ;
21
+ use Magento \Framework \Pricing \Helper \Data ;
22
+ use Magento \Framework \Serialize \Serializer \Json ;
11
23
12
24
/**
13
25
* Helper for fetching properties by product configuration item
@@ -19,61 +31,67 @@ class Configuration extends AbstractHelper implements ConfigurationInterface
19
31
/**
20
32
* Core data
21
33
*
22
- * @var \Magento\Framework\Pricing\Helper\ Data
34
+ * @var Data
23
35
*/
24
36
protected $ pricingHelper ;
25
37
26
38
/**
27
39
* Catalog product configuration
28
40
*
29
- * @var \Magento\Catalog\Helper\Product\Configuration
41
+ * @var ProductConfiguration
30
42
*/
31
43
protected $ productConfiguration ;
32
44
33
45
/**
34
- * Escaper
35
- *
36
- * @var \Magento\Framework\Escaper
46
+ * @var Escaper
37
47
*/
38
48
protected $ escaper ;
39
49
40
50
/**
41
51
* Serializer interface instance.
42
52
*
43
- * @var \Magento\Framework\Serialize\Serializer\ Json
53
+ * @var Json
44
54
*/
45
55
private $ serializer ;
46
56
47
57
/**
48
- * @param \Magento\Framework\App\Helper\Context $context
49
- * @param \Magento\Catalog\Helper\Product\Configuration $productConfiguration
50
- * @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
51
- * @param \Magento\Framework\Escaper $escaper
52
- * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
58
+ * @var TaxPrice
59
+ */
60
+ private $ taxHelper ;
61
+
62
+ /**
63
+ * @param Context $context
64
+ * @param ProductConfiguration $productConfiguration
65
+ * @param Data $pricingHelper
66
+ * @param Escaper $escaper
67
+ * @param Json|null $serializer
68
+ * @param TaxPrice|null $taxHelper
53
69
*/
54
70
public function __construct (
55
- \Magento \Framework \App \Helper \Context $ context ,
56
- \Magento \Catalog \Helper \Product \Configuration $ productConfiguration ,
57
- \Magento \Framework \Pricing \Helper \Data $ pricingHelper ,
58
- \Magento \Framework \Escaper $ escaper ,
59
- \Magento \Framework \Serialize \Serializer \Json $ serializer = null
71
+ Context $ context ,
72
+ ProductConfiguration $ productConfiguration ,
73
+ Data $ pricingHelper ,
74
+ Escaper $ escaper ,
75
+ Json $ serializer = null ,
76
+ TaxPrice $ taxHelper = null
60
77
) {
61
78
$ this ->productConfiguration = $ productConfiguration ;
62
79
$ this ->pricingHelper = $ pricingHelper ;
63
80
$ this ->escaper = $ escaper ;
64
- $ this ->serializer = $ serializer ?: \Magento \Framework \App \ObjectManager::getInstance ()
65
- ->get (\Magento \Framework \Serialize \Serializer \Json::class);
81
+ $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()
82
+ ->get (Json::class);
83
+ $ this ->taxHelper = $ taxHelper ?? ObjectManager::getInstance ()->get (TaxPrice::class);
66
84
parent ::__construct ($ context );
67
85
}
68
86
69
87
/**
70
88
* Get selection quantity
71
89
*
72
- * @param \Magento\Catalog\Model\ Product $product
90
+ * @param Product $product
73
91
* @param int $selectionId
74
92
* @return float
75
93
*/
76
- public function getSelectionQty (\ Magento \ Catalog \ Model \ Product $ product , $ selectionId )
94
+ public function getSelectionQty (Product $ product , $ selectionId )
77
95
{
78
96
$ selectionQty = $ product ->getCustomOption ('selection_qty_ ' . $ selectionId );
79
97
if ($ selectionQty ) {
@@ -86,15 +104,15 @@ public function getSelectionQty(\Magento\Catalog\Model\Product $product, $select
86
104
* Obtain final price of selection in a bundle product
87
105
*
88
106
* @param ItemInterface $item
89
- * @param \Magento\Catalog\Model\ Product $selectionProduct
107
+ * @param Product $selectionProduct
90
108
* @return float
91
109
*/
92
- public function getSelectionFinalPrice (ItemInterface $ item , \ Magento \ Catalog \ Model \ Product $ selectionProduct )
110
+ public function getSelectionFinalPrice (ItemInterface $ item , Product $ selectionProduct )
93
111
{
94
112
$ selectionProduct ->unsetData ('final_price ' );
95
113
96
114
$ product = $ item ->getProduct ();
97
- /** @var \Magento\Bundle\Model\Product\ Price $price */
115
+ /** @var Price $price */
98
116
$ price = $ product ->getPriceModel ();
99
117
100
118
return $ price ->getSelectionFinalTotalPrice (
@@ -121,7 +139,7 @@ public function getBundleOptions(ItemInterface $item)
121
139
$ options = [];
122
140
$ product = $ item ->getProduct ();
123
141
124
- /** @var \Magento\Bundle\Model\Product\ Type $typeInstance */
142
+ /** @var Type $typeInstance */
125
143
$ typeInstance = $ product ->getTypeInstance ();
126
144
127
145
// get bundle options
@@ -150,16 +168,7 @@ public function getBundleOptions(ItemInterface $item)
150
168
$ bundleSelections = $ bundleOption ->getSelections ();
151
169
152
170
foreach ($ bundleSelections as $ bundleSelection ) {
153
- $ qty = $ this ->getSelectionQty ($ product , $ bundleSelection ->getSelectionId ()) * 1 ;
154
- if ($ qty ) {
155
- $ option ['value ' ][] = $ qty . ' x '
156
- . $ this ->escaper ->escapeHtml ($ bundleSelection ->getName ())
157
- . ' '
158
- . $ this ->pricingHelper ->currency (
159
- $ this ->getSelectionFinalPrice ($ item , $ bundleSelection )
160
- );
161
- $ option ['has_html ' ] = true ;
162
- }
171
+ $ option = $ this ->getOptionPriceHtml ($ item , $ bundleSelection , $ option );
163
172
}
164
173
165
174
if ($ option ['value ' ]) {
@@ -173,6 +182,48 @@ public function getBundleOptions(ItemInterface $item)
173
182
return $ options ;
174
183
}
175
184
185
+ /**
186
+ * Get bundle options' prices
187
+ *
188
+ * @param ItemInterface $item
189
+ * @param ProductInterface $bundleSelection
190
+ * @param array $option
191
+ * @return array
192
+ * @throws LocalizedException
193
+ */
194
+ private function getOptionPriceHtml (ItemInterface $ item , ProductInterface $ bundleSelection , array $ option ): array
195
+ {
196
+ $ product = $ item ->getProduct ();
197
+ $ qty = $ this ->getSelectionQty ($ item ->getProduct (), $ bundleSelection ->getSelectionId ()) * 1 ;
198
+ if ($ qty ) {
199
+ $ selectionPrice = $ this ->getSelectionFinalPrice ($ item , $ bundleSelection );
200
+
201
+ $ displayCartPricesBoth = $ this ->taxHelper ->displayCartPricesBoth ();
202
+ if ($ displayCartPricesBoth ) {
203
+ $ selectionFinalPrice =
204
+ $ this ->taxHelper
205
+ ->getTaxPrice ($ product , $ selectionPrice , true );
206
+ $ selectionFinalPriceExclTax =
207
+ $ this ->taxHelper
208
+ ->getTaxPrice ($ product , $ selectionPrice , false );
209
+ } else {
210
+ $ selectionFinalPrice = $ this ->taxHelper ->getTaxPrice ($ item ->getProduct (), $ selectionPrice );
211
+ }
212
+ $ option ['value ' ][] = $ qty . ' x '
213
+ . $ this ->escaper ->escapeHtml ($ bundleSelection ->getName ())
214
+ . ' '
215
+ . $ this ->pricingHelper ->currency (
216
+ $ selectionFinalPrice
217
+ )
218
+ . ($ displayCartPricesBoth ? ' ' . __ ('Excl. tax: ' ) . ' '
219
+ . $ this ->pricingHelper ->currency (
220
+ $ selectionFinalPriceExclTax
221
+ ) : '' );
222
+ $ option ['has_html ' ] = true ;
223
+ }
224
+ return $ option ;
225
+ }
226
+
176
227
/**
177
228
* Retrieves product options list
178
229
*
0 commit comments