5
5
*/
6
6
namespace Magento \Tax \Observer ;
7
7
8
- use Magento \Framework \Event \ObserverInterface ;
9
8
use Magento \Catalog \Pricing \Price \BasePrice ;
10
9
use Magento \Catalog \Pricing \Price \RegularPrice ;
10
+ use Magento \Framework \Event \ObserverInterface ;
11
11
12
+ /**
13
+ * Modifies the bundle config for the front end to resemble the tax included price when tax included prices.
14
+ */
12
15
class GetPriceConfigurationObserver implements ObserverInterface
13
16
{
14
17
/**
@@ -23,6 +26,11 @@ class GetPriceConfigurationObserver implements ObserverInterface
23
26
*/
24
27
protected $ registry ;
25
28
29
+ /**
30
+ * @var array Cache of the current bundle selection items
31
+ */
32
+ private $ selectionCache = [];
33
+
26
34
/**
27
35
* @param \Magento\Framework\Registry $registry
28
36
* @param \Magento\Tax\Helper\Data $taxData
@@ -44,6 +52,7 @@ public function __construct(
44
52
*/
45
53
public function execute (\Magento \Framework \Event \Observer $ observer )
46
54
{
55
+ $ this ->selectionCache = [];
47
56
if ($ this ->taxData ->displayPriceIncludingTax ()) {
48
57
/** @var \Magento\Catalog\Model\Product $product */
49
58
$ product = $ this ->registry ->registry ('current_product ' );
@@ -78,12 +87,11 @@ private function recurConfigAndUpdatePrice($input, $searchKey)
78
87
if (is_array ($ el )) {
79
88
$ holder [$ key ] =
80
89
$ this ->recurConfigAndUpdatePrice ($ el , $ searchKey );
81
- if ($ key === $ searchKey ) {
82
- if ((array_key_exists ('basePrice ' , $ holder [$ key ]))) {
83
- if (array_key_exists ('optionId ' , $ input )) {
84
- $ holder = $ this ->updatePriceForBundle ($ holder , $ key );
85
- }
86
- }
90
+ if ($ key === $ searchKey
91
+ && array_key_exists ('optionId ' , $ input )
92
+ && array_key_exists ('basePrice ' , $ holder [$ key ])
93
+ ) {
94
+ $ holder = $ this ->updatePriceForBundle ($ holder , $ key );
87
95
}
88
96
} else {
89
97
$ holder [$ key ] = $ el ;
@@ -102,32 +110,35 @@ private function recurConfigAndUpdatePrice($input, $searchKey)
102
110
*/
103
111
private function updatePriceForBundle ($ holder , $ key )
104
112
{
105
- if (array_key_exists ($ key , $ holder )) {
106
- if (array_key_exists ('basePrice ' , $ holder [$ key ])) {
107
- /** @var \Magento\Catalog\Model\Product $product */
108
- $ product = $ this ->registry ->registry ('current_product ' );
109
- if ($ product ->getTypeId () == \Magento \Catalog \Model \Product \Type::TYPE_BUNDLE ) {
113
+ if (array_key_exists ($ key , $ holder )
114
+ && array_key_exists ('basePrice ' , $ holder [$ key ])) {
115
+ /** @var \Magento\Catalog\Model\Product $product */
116
+ $ product = $ this ->registry ->registry ('current_product ' );
117
+ if ($ product ->getTypeId () == \Magento \Catalog \Model \Product \Type::TYPE_BUNDLE ) {
118
+ if (!isset ($ this ->selectionCache [$ product ->getId ()])) {
110
119
$ typeInstance = $ product ->getTypeInstance ();
111
120
$ typeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
112
121
113
122
$ selectionCollection = $ typeInstance ->getSelectionsCollection (
114
123
$ typeInstance ->getOptionsIds ($ product ),
115
124
$ product
116
125
);
126
+ $ this ->selectionCache [$ product ->getId ()] = $ selectionCollection ->getItems ();
127
+ }
128
+ $ arrSelections = $ this ->selectionCache [$ product ->getId ()];
117
129
118
- foreach ($ selectionCollection -> getItems () as $ selectionItem ) {
119
- if ($ holder ['optionId ' ] == $ selectionItem ->getId ()) {
120
- /** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
121
- $ baseAmount = $ selectionItem ->getPriceInfo ()->getPrice (BasePrice::PRICE_CODE )->getAmount ();
122
- /** @var \Magento\Framework\Pricing\Amount\Base $oldAmount */
123
- $ oldAmount =
130
+ foreach ($ arrSelections as $ selectionItem ) {
131
+ if ($ holder ['optionId ' ] == $ selectionItem ->getId ()) {
132
+ /** @var \Magento\Framework\Pricing\Amount\Base $baseAmount */
133
+ $ baseAmount = $ selectionItem ->getPriceInfo ()->getPrice (BasePrice::PRICE_CODE )->getAmount ();
134
+ /** @var \Magento\Framework\Pricing\Amount\Base $oldAmount */
135
+ $ oldAmount =
124
136
$ selectionItem ->getPriceInfo ()->getPrice (RegularPrice::PRICE_CODE )->getAmount ();
125
- if ($ baseAmount ->hasAdjustment ('tax ' )) {
126
- $ holder [$ key ]['basePrice ' ]['amount ' ] =
137
+ if ($ baseAmount ->hasAdjustment ('tax ' )) {
138
+ $ holder [$ key ]['basePrice ' ]['amount ' ] =
127
139
$ baseAmount ->getBaseAmount () + $ baseAmount ->getAdjustmentAmount ('tax ' );
128
- $ holder [$ key ]['oldPrice ' ]['amount ' ] =
140
+ $ holder [$ key ]['oldPrice ' ]['amount ' ] =
129
141
$ oldAmount ->getBaseAmount () + $ oldAmount ->getAdjustmentAmount ('tax ' );
130
- }
131
142
}
132
143
}
133
144
}
0 commit comments