Skip to content

Commit bbce379

Browse files
2 parents de089e9 + 8868a95 commit bbce379

File tree

65 files changed

+1889
-287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1889
-287
lines changed

app/code/Magento/Backend/Block/Widget/Grid/Massaction/AbstractMassaction.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,16 @@ public function getGridIdsJson()
275275
if (!$this->getUseSelectAll()) {
276276
return '';
277277
}
278-
279278
/** @var \Magento\Framework\Data\Collection $allIdsCollection */
280279
$allIdsCollection = clone $this->getParentBlock()->getCollection();
281-
$gridIds = $allIdsCollection->clear()->setPageSize(0)->getAllIds();
282-
280+
281+
if ($this->getMassactionIdField()) {
282+
$massActionIdField = $this->getMassactionIdField();
283+
} else {
284+
$massActionIdField = $this->getParentBlock()->getMassactionIdField();
285+
}
286+
287+
$gridIds = $allIdsCollection->setPageSize(0)->getColumnValues($massActionIdField);
283288
if (!empty($gridIds)) {
284289
return join(",", $gridIds);
285290
}

app/code/Magento/Backend/Block/Widget/Grid/Massaction/Extended.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,14 @@ public function getGridIdsJson()
274274

275275
/** @var \Magento\Framework\Data\Collection $allIdsCollection */
276276
$allIdsCollection = clone $this->getParentBlock()->getCollection();
277-
$gridIds = $allIdsCollection->clear()->setPageSize(0)->getAllIds();
277+
278+
if ($this->getMassactionIdField()) {
279+
$massActionIdField = $this->getMassactionIdField();
280+
} else {
281+
$massActionIdField = $this->getParentBlock()->getMassactionIdField();
282+
}
283+
284+
$gridIds = $allIdsCollection->setPageSize(0)->getColumnValues($massActionIdField);
278285

279286
if (!empty($gridIds)) {
280287
return join(",", $gridIds);

app/code/Magento/Backend/Test/Unit/Block/Widget/Grid/Massaction/ExtendedTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ public function testGetGridIdsJsonWithoutUseSelectAll()
132132
public function testGetGridIdsJsonWithUseSelectAll(array $items, $result)
133133
{
134134
$this->_block->setUseSelectAll(true);
135-
135+
136+
if ($this->_block->getMassactionIdField()) {
137+
$massActionIdField = $this->_block->getMassactionIdField();
138+
} else {
139+
$massActionIdField = $this->_block->getParentBlock()->getMassactionIdField();
140+
}
141+
136142
$collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
137143
->disableOriginalConstructor()
138144
->getMock();
@@ -141,15 +147,13 @@ public function testGetGridIdsJsonWithUseSelectAll(array $items, $result)
141147
->method('getCollection')
142148
->willReturn($collectionMock);
143149

144-
$collectionMock->expects($this->once())
145-
->method('clear')
146-
->willReturnSelf();
147150
$collectionMock->expects($this->once())
148151
->method('setPageSize')
149152
->with(0)
150153
->willReturnSelf();
151154
$collectionMock->expects($this->once())
152-
->method('getAllIds')
155+
->method('getColumnValues')
156+
->with($massActionIdField)
153157
->willReturn($items);
154158

155159
$this->assertEquals($result, $this->_block->getGridIdsJson());

app/code/Magento/Backend/Test/Unit/Block/Widget/Grid/MassactionTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ public function testGetGridIdsJsonWithoutUseSelectAll()
237237
public function testGetGridIdsJsonWithUseSelectAll(array $items, $result)
238238
{
239239
$this->_block->setUseSelectAll(true);
240+
241+
if ($this->_block->getMassactionIdField()) {
242+
$massActionIdField = $this->_block->getMassactionIdField();
243+
} else {
244+
$massActionIdField = $this->_block->getParentBlock()->getMassactionIdField();
245+
}
240246

241247
$collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
242248
->disableOriginalConstructor()
@@ -245,16 +251,13 @@ public function testGetGridIdsJsonWithUseSelectAll(array $items, $result)
245251
$this->_gridMock->expects($this->once())
246252
->method('getCollection')
247253
->willReturn($collectionMock);
248-
249-
$collectionMock->expects($this->once())
250-
->method('clear')
251-
->willReturnSelf();
252254
$collectionMock->expects($this->once())
253255
->method('setPageSize')
254256
->with(0)
255257
->willReturnSelf();
256258
$collectionMock->expects($this->once())
257-
->method('getAllIds')
259+
->method('getColumnValues')
260+
->with($massActionIdField)
258261
->willReturn($items);
259262

260263
$this->assertEquals($result, $this->_block->getGridIdsJson());

app/code/Magento/Bundle/Block/Catalog/Product/View/Type/Bundle/Option.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,16 @@ protected function _getSelectedOptions()
143143
{
144144
if (is_null($this->_selectedOptions)) {
145145
$this->_selectedOptions = [];
146+
147+
/** @var \Magento\Bundle\Model\Option $option */
146148
$option = $this->getOption();
147149

148150
if ($this->getProduct()->hasPreconfiguredValues()) {
149-
$configValue = $this->getProduct()->getPreconfiguredValues()->getData(
151+
$selectionId = $this->getProduct()->getPreconfiguredValues()->getData(
150152
'bundle_option/' . $option->getId()
151153
);
152-
if ($configValue) {
153-
$this->_selectedOptions = $configValue;
154+
if ($selectionId && $option->getSelectionById($selectionId)) {
155+
$this->_selectedOptions = $selectionId;
154156
} elseif (!$option->getRequired()) {
155157
$this->_selectedOptions = 'None';
156158
}

app/code/Magento/Bundle/Controller/Adminhtml/Product/Initialization/Helper/Plugin/Bundle.php

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ protected function processBundleOptionsData(\Magento\Catalog\Model\Product $prod
133133

134134
$option = $this->optionFactory->create(['data' => $optionData]);
135135
$option->setSku($product->getSku());
136-
$option->setOptionId(null);
137136

138137
$links = [];
139138
$bundleLinks = $product->getBundleSelectionsData();
@@ -142,28 +141,13 @@ protected function processBundleOptionsData(\Magento\Catalog\Model\Product $prod
142141
}
143142

144143
foreach ($bundleLinks[$key] as $linkData) {
145-
if ((bool)$linkData['delete']) {
144+
if (!empty($linkData['delete'])) {
146145
continue;
147146
}
148-
$link = $this->linkFactory->create(['data' => $linkData]);
149-
150-
if ((int)$product->getPriceType() !== \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC) {
151-
if (array_key_exists('selection_price_value', $linkData)) {
152-
$link->setPrice($linkData['selection_price_value']);
153-
}
154-
if (array_key_exists('selection_price_type', $linkData)) {
155-
$link->setPriceType($linkData['selection_price_type']);
156-
}
147+
if (!empty($linkData['selection_id'])) {
148+
$linkData['id'] = $linkData['selection_id'];
157149
}
158-
159-
$linkProduct = $this->productRepository->getById($linkData['product_id']);
160-
$link->setSku($linkProduct->getSku());
161-
$link->setQty($linkData['selection_qty']);
162-
163-
if (array_key_exists('selection_can_change_qty', $linkData)) {
164-
$link->setCanChangeQuantity($linkData['selection_can_change_qty']);
165-
}
166-
$links[] = $link;
150+
$links[] = $this->buildLink($product, $linkData);
167151
}
168152
$option->setProductLinks($links);
169153
$options[] = $option;
@@ -203,9 +187,40 @@ protected function processDynamicOptionsData(\Magento\Catalog\Model\Product $pro
203187
}
204188
$customOption = $this->customOptionFactory->create(['data' => $customOptionData]);
205189
$customOption->setProductSku($product->getSku());
206-
$customOption->setOptionId(null);
207190
$newOptions[] = $customOption;
208191
}
209192
$product->setOptions($newOptions);
210193
}
194+
195+
/**
196+
* @param \Magento\Catalog\Model\Product $product
197+
* @param array $linkData
198+
*
199+
* @return \Magento\Bundle\Api\Data\LinkInterface
200+
*/
201+
private function buildLink(
202+
\Magento\Catalog\Model\Product $product,
203+
array $linkData
204+
) {
205+
$link = $this->linkFactory->create(['data' => $linkData]);
206+
207+
if ((int)$product->getPriceType() !== \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC) {
208+
if (array_key_exists('selection_price_value', $linkData)) {
209+
$link->setPrice($linkData['selection_price_value']);
210+
}
211+
if (array_key_exists('selection_price_type', $linkData)) {
212+
$link->setPriceType($linkData['selection_price_type']);
213+
}
214+
}
215+
216+
$linkProduct = $this->productRepository->getById($linkData['product_id']);
217+
$link->setSku($linkProduct->getSku());
218+
$link->setQty($linkData['selection_qty']);
219+
220+
if (array_key_exists('selection_can_change_qty', $linkData)) {
221+
$link->setCanChangeQuantity($linkData['selection_can_change_qty']);
222+
}
223+
224+
return $link;
225+
}
211226
}

app/code/Magento/Bundle/Model/LinkManagement.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ public function saveChild(
168168
* @param string $linkedProductId
169169
* @param string $parentProductId
170170
* @return \Magento\Bundle\Model\Selection
171+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
172+
* @SuppressWarnings(PHPMD.NPathComplexity)
171173
*/
172174
protected function mapProductLinkToSelectionModel(
173175
\Magento\Bundle\Model\Selection $selectionModel,
@@ -177,7 +179,10 @@ protected function mapProductLinkToSelectionModel(
177179
) {
178180
$selectionModel->setProductId($linkedProductId);
179181
$selectionModel->setParentProductId($parentProductId);
180-
if (($productLink->getOptionId() !== null)) {
182+
if ($productLink->getSelectionId() !== null) {
183+
$selectionModel->setSelectionId($productLink->getSelectionId());
184+
}
185+
if ($productLink->getOptionId() !== null) {
181186
$selectionModel->setOptionId($productLink->getOptionId());
182187
}
183188
if ($productLink->getPosition() !== null) {
@@ -217,8 +222,13 @@ public function addChild(
217222
);
218223
}
219224

225+
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
226+
220227
$options = $this->optionCollection->create();
228+
221229
$options->setIdFilter($optionId);
230+
$options->setProductLinkFilter($product->getData($linkField));
231+
222232
$existingOption = $options->getFirstItem();
223233

224234
if (!$existingOption->getId()) {
@@ -230,7 +240,6 @@ public function addChild(
230240
);
231241
}
232242

233-
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
234243
/* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */
235244
$resource = $this->bundleFactory->create();
236245
$selections = $resource->getSelectionsData($product->getData($linkField));
@@ -243,7 +252,8 @@ public function addChild(
243252
if ($selections) {
244253
foreach ($selections as $selection) {
245254
if ($selection['option_id'] == $optionId &&
246-
$selection['product_id'] == $linkProductModel->getEntityId()) {
255+
$selection['product_id'] == $linkProductModel->getEntityId() &&
256+
$selection['parent_product_id'] == $product->getData($linkField)) {
247257
if (!$product->getCopyFromView()) {
248258
throw new CouldNotSaveException(
249259
__(
@@ -300,7 +310,6 @@ public function removeChild($sku, $optionId, $childSku)
300310
continue;
301311
}
302312
$excludeSelectionIds[] = $selection->getSelectionId();
303-
$usedProductIds[] = $selection->getProductId();
304313
}
305314
}
306315
if (empty($removeSelectionIds)) {

app/code/Magento/Bundle/Model/OptionRepository.php

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,34 @@ public function save(
187187
$metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
188188

189189
$option->setStoreId($product->getStoreId());
190-
$option->setParentId($product->getData($metadata->getLinkField()));
190+
$parentId = $product->getData($metadata->getLinkField());
191+
$option->setParentId($parentId);
192+
193+
$optionId = $option->getOptionId();
191194
$linksToAdd = [];
192-
$option->setDefaultTitle($option->getDefaultTitle() ?: $option->getTitle());
193-
if (is_array($option->getProductLinks())) {
194-
$linksToAdd = $option->getProductLinks();
195+
$optionCollection = $this->type->getOptionsCollection($product);
196+
$optionCollection->setIdFilter($option->getOptionId());
197+
$optionCollection->setProductLinkFilter($parentId);
198+
199+
/** @var \Magento\Bundle\Model\Option $existingOption */
200+
$existingOption = $optionCollection->getFirstItem();
201+
if (!$optionId) {
202+
$option->setOptionId(null);
203+
}
204+
if (!$optionId || $existingOption->getParentId() != $parentId) {
205+
$option->setDefaultTitle($option->getTitle());
206+
if (is_array($option->getProductLinks())) {
207+
$linksToAdd = $option->getProductLinks();
208+
}
209+
} else {
210+
if (!$existingOption->getOptionId()) {
211+
throw new NoSuchEntityException('Requested option doesn\'t exist');
212+
}
213+
214+
$option->setData(array_merge($existingOption->getData(), $option->getData()));
215+
$this->updateOptionSelection($product, $option);
195216
}
217+
196218
try {
197219
$this->optionResource->save($option);
198220
} catch (\Exception $e) {
@@ -218,16 +240,25 @@ protected function updateOptionSelection(
218240
\Magento\Catalog\Api\Data\ProductInterface $product,
219241
\Magento\Bundle\Api\Data\OptionInterface $option
220242
) {
221-
$existingLinks = [];
243+
$optionId = $option->getOptionId();
244+
$existingLinks = $this->linkManagement->getChildren($product->getSku(), $optionId);
222245
$linksToAdd = [];
246+
$linksToUpdate = [];
223247
$linksToDelete = [];
224248
if (is_array($option->getProductLinks())) {
225249
$productLinks = $option->getProductLinks();
226250
foreach ($productLinks as $productLink) {
227-
$linksToAdd[] = $productLink;
251+
if (!$productLink->getId() && !$productLink->getSelectionId()) {
252+
$linksToAdd[] = $productLink;
253+
} else {
254+
$linksToUpdate[] = $productLink;
255+
}
228256
}
229257
/** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */
230-
$linksToDelete = $this->compareLinks([], $existingLinks);
258+
$linksToDelete = $this->compareLinks($existingLinks, $linksToUpdate);
259+
}
260+
foreach ($linksToUpdate as $linkedProduct) {
261+
$this->linkManagement->saveChild($product->getSku(), $linkedProduct);
231262
}
232263
foreach ($linksToDelete as $linkedProduct) {
233264
$this->linkManagement->removeChild(
@@ -257,33 +288,36 @@ private function getProduct($sku)
257288
}
258289

259290
/**
260-
* Computes the difference of arrays
291+
* Computes the difference between given arrays.
292+
*
293+
* @param \Magento\Bundle\Api\Data\LinkInterface[] $firstArray
294+
* @param \Magento\Bundle\Api\Data\LinkInterface[] $secondArray
261295
*
262-
* @param array $firstArray of \Magento\Bundle\Api\Data\LinkInterface
263-
* @param array $secondArray of \Magento\Bundle\Api\Data\LinkInterface
264296
* @return array
265-
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
266297
*/
267-
private function compareLinks(
268-
array $firstArray,
269-
array $secondArray
270-
) {
298+
private function compareLinks(array $firstArray, array $secondArray)
299+
{
271300
$result = [];
272-
if (count($firstArray) < count($secondArray)) {
273-
$holder = $firstArray;
274-
$firstArray = $secondArray;
275-
$secondArray = $holder;
301+
302+
$firstArrayIds = [];
303+
$firstArrayMap = [];
304+
305+
$secondArrayIds = [];
306+
307+
foreach ($firstArray as $item) {
308+
$firstArrayIds[] = $item->getId();
309+
310+
$firstArrayMap[$item->getId()] = $item;
276311
}
277-
foreach ($firstArray as $obj) {
278-
foreach ($secondArray as $objToCompare) {
279-
if ($obj->getId() != $objToCompare->getId()
280-
&& $obj instanceof \Magento\Bundle\Api\Data\LinkInterface
281-
&& $objToCompare instanceof \Magento\Bundle\Api\Data\LinkInterface
282-
) {
283-
$result[] = $obj;
284-
}
285-
}
312+
313+
foreach ($secondArray as $item) {
314+
$secondArrayIds[] = $item->getId();
286315
}
316+
317+
foreach (array_diff($firstArrayIds, $secondArrayIds) as $id) {
318+
$result[] = $firstArrayMap[$id];
319+
}
320+
287321
return $result;
288322
}
289323

0 commit comments

Comments
 (0)