@@ -86,9 +86,11 @@ public function execute($entity, $arguments = [])
86
86
$ identifierField = $ this ->metadataPoll ->getMetadata (ProductInterface::class)->getLinkField ();
87
87
$ priceRows = array_filter ($ priceRows );
88
88
$ productId = (int ) $ entity ->getData ($ identifierField );
89
+ $ pricesStored = $ this ->getPricesStored ($ priceRows );
90
+ $ pricesMerged = $ this ->mergePrices ($ priceRows , $ pricesStored );
89
91
90
92
// prepare and save data
91
- foreach ($ priceRows as $ data ) {
93
+ foreach ($ pricesMerged as $ data ) {
92
94
$ isPriceWebsiteGlobal = (int )$ data ['website_id ' ] === 0 ;
93
95
if ($ isGlobal === $ isPriceWebsiteGlobal
94
96
|| !empty ($ data ['price_qty ' ])
@@ -109,4 +111,51 @@ public function execute($entity, $arguments = [])
109
111
110
112
return $ entity ;
111
113
}
114
+
115
+ /**
116
+ * Merge prices
117
+ *
118
+ * @param array $prices
119
+ * @param array $pricesStored
120
+ * @return array
121
+ */
122
+ private function mergePrices (array $ prices , array $ pricesStored ): array
123
+ {
124
+ if (!$ pricesStored ) {
125
+ return $ prices ;
126
+ }
127
+ $ pricesId = [];
128
+ $ pricesStoredId = [];
129
+ foreach ($ prices as $ price ) {
130
+ if (isset ($ price ['price_id ' ])) {
131
+ $ pricesId [$ price ['price_id ' ]] = $ price ;
132
+ }
133
+ }
134
+ foreach ($ pricesStored as $ price ) {
135
+ if (isset ($ price ['price_id ' ])) {
136
+ $ pricesStoredId [$ price ['price_id ' ]] = $ price ;
137
+ }
138
+ }
139
+ $ pricesAdd = array_diff_key ($ pricesStoredId , $ pricesId );
140
+ foreach ($ pricesAdd as $ price ) {
141
+ $ prices [] = $ price ;
142
+ }
143
+ return $ prices ;
144
+ }
145
+
146
+ /**
147
+ * Get stored prices
148
+ *
149
+ * @param array $prices
150
+ * @return array
151
+ */
152
+ private function getPricesStored (array $ prices ): array
153
+ {
154
+ $ pricesStored = [];
155
+ $ price = reset ($ prices );
156
+ if (isset ($ price ['product_id ' ]) && $ price ['product_id ' ]) {
157
+ $ pricesStored = $ this ->tierPriceResource ->loadPriceData ($ price ['product_id ' ]);
158
+ }
159
+ return $ pricesStored ;
160
+ }
112
161
}
0 commit comments