Skip to content

Commit 101a121

Browse files
author
Michal Derlatka
committed
#26110: Bundle products options input validation
1 parent 28e20d9 commit 101a121

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

app/code/Magento/Bundle/Model/Product/Type.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,13 @@ public function beforeSave($product)
416416
if ($product->getCanSaveBundleSelections()) {
417417
$product->canAffectOptions(true);
418418
$selections = $product->getBundleSelectionsData();
419-
if ($selections && !empty($selections)) {
420-
$options = $product->getBundleOptionsData();
421-
if ($options) {
422-
foreach ($options as $option) {
423-
if (empty($option['delete']) || 1 != (int)$option['delete']) {
424-
$product->setTypeHasOptions(true);
425-
if (1 == (int)$option['required']) {
426-
$product->setTypeHasRequiredOptions(true);
427-
break;
428-
}
419+
if (!empty($selections) && $options = $product->getBundleOptionsData()) {
420+
foreach ($options as $option) {
421+
if (empty($option['delete']) || 1 != (int)$option['delete']) {
422+
$product->setTypeHasOptions(true);
423+
if (1 == (int)$option['required']) {
424+
$product->setTypeHasRequiredOptions(true);
425+
break;
429426
}
430427
}
431428
}
@@ -461,7 +458,7 @@ public function getOptionsIds($product)
461458
* Retrieve bundle option collection
462459
*
463460
* @param \Magento\Catalog\Model\Product $product
464-
* @return Collection
461+
* @return \Magento\Bundle\Model\ResourceModel\Option\Collection
465462
*/
466463
public function getOptionsCollection($product)
467464
{
@@ -532,10 +529,10 @@ public function getSelectionsCollection($optionIds, $product)
532529
* Example: the catalog inventory validation of decimal qty can change qty to int,
533530
* so need to change quote item qty option value too.
534531
*
535-
* @param array $options
536-
* @param \Magento\Framework\DataObject $option
537-
* @param mixed $value
538-
* @param \Magento\Catalog\Model\Product $product
532+
* @param array $options
533+
* @param \Magento\Framework\DataObject $option
534+
* @param mixed $value
535+
* @param \Magento\Catalog\Model\Product $product
539536
* @return $this
540537
*/
541538
public function updateQtyOption($options, \Magento\Framework\DataObject $option, $value, $product)
@@ -894,7 +891,7 @@ public function getSelectionsByIds($selectionIds, $product)
894891
*
895892
* @param array $optionIds
896893
* @param \Magento\Catalog\Model\Product $product
897-
* @return Collection
894+
* @return \Magento\Bundle\Model\ResourceModel\Option\Collection
898895
*/
899896
public function getOptionsByIds($optionIds, $product)
900897
{
@@ -1191,9 +1188,11 @@ public function canConfigure($product)
11911188
* @return void
11921189
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
11931190
*/
1191+
// @codingStandardsIgnoreStart
11941192
public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
11951193
{
11961194
}
1195+
// @codingStandardsIgnoreEnd
11971196

11981197
/**
11991198
* Return array of specific to type product entities
@@ -1203,18 +1202,19 @@ public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
12031202
*/
12041203
public function getIdentities(\Magento\Catalog\Model\Product $product)
12051204
{
1206-
$identities = parent::getIdentities($product);
1205+
$identities = [];
1206+
$identities[] = parent::getIdentities($product);
12071207
/** @var \Magento\Bundle\Model\Option $option */
12081208
foreach ($this->getOptions($product) as $option) {
12091209
if ($option->getSelections()) {
12101210
/** @var \Magento\Catalog\Model\Product $selection */
12111211
foreach ($option->getSelections() as $selection) {
1212-
$identities = array_merge($identities, $selection->getIdentities());
1212+
$identities[] = $selection->getIdentities();
12131213
}
12141214
}
12151215
}
12161216

1217-
return $identities;
1217+
return array_merge([], ...$identities);
12181218
}
12191219

12201220
/**
@@ -1261,7 +1261,7 @@ protected function getBeforeQty($product, $selection)
12611261
*
12621262
* @param \Magento\Catalog\Model\Product $product
12631263
* @param bool $isStrictProcessMode
1264-
* @param Collection $optionsCollection
1264+
* @param \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection
12651265
* @param int[] $options
12661266
* @return void
12671267
* @throws \Magento\Framework\Exception\LocalizedException
@@ -1331,7 +1331,7 @@ private function isSelectedOptionValid($option, $options): bool
13311331
*
13321332
* @param \Magento\Bundle\Model\ResourceModel\Selection\Collection $selections
13331333
* @param bool $skipSaleableCheck
1334-
* @param Collection $optionsCollection
1334+
* @param \Magento\Bundle\Model\ResourceModel\Option\Collection $optionsCollection
13351335
* @param int[] $options
13361336
* @return void
13371337
* @throws \Magento\Framework\Exception\LocalizedException
@@ -1387,16 +1387,18 @@ protected function checkIsResult($_result)
13871387
*/
13881388
protected function mergeSelectionsWithOptions($options, $selections)
13891389
{
1390+
$selections = [];
1391+
13901392
foreach ($options as $option) {
13911393
$optionSelections = $option->getSelections();
13921394
if ($option->getRequired() && is_array($optionSelections) && count($optionSelections) == 1) {
1393-
$selections = array_merge($selections, $optionSelections);
1395+
$selections[] = $optionSelections;
13941396
} else {
13951397
$selections = [];
13961398
break;
13971399
}
13981400
}
13991401

1400-
return $selections;
1402+
return array_merge([], ...$selections);
14011403
}
14021404
}

dev/tests/integration/testsuite/Magento/Bundle/_files/product_with_multiple_options_radio_select.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
require __DIR__ . '/../../../Magento/Catalog/_files/multiple_products.php';
7+
use Magento\Catalog\Model\Product;
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
810

9-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
11+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/multiple_products.php');
12+
13+
$objectManager = Bootstrap::getObjectManager();
1014

1115
$productIds = range(10, 12, 1);
1216
foreach ($productIds as $productId) {
@@ -24,8 +28,8 @@
2428
$stockItem->save();
2529
}
2630

27-
/** @var $product \Magento\Catalog\Model\Product */
28-
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
31+
/** @var $product Product */
32+
$product = $objectManager->create(Product::class);
2933
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)
3034
->setId(3)
3135
->setAttributeSetId(4)
@@ -35,7 +39,6 @@
3539
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
3640
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
3741
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
38-
->setPriceView(1)
3942
->setPriceType(1)
4043
->setPrice(10.0)
4144
->setShipmentType(0)

dev/tests/integration/testsuite/Magento/Bundle/_files/product_with_multiple_options_radio_select_rollback.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
use Magento\TestFramework\Helper\Bootstrap;
7+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
68

7-
require __DIR__ . '/../../../Magento/Catalog/_files/multiple_products_rollback.php';
9+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/multiple_products_rollback.php');
810

11+
$objectManager = Bootstrap::getObjectManager();
912
/** @var \Magento\Framework\Registry $registry */
10-
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
13+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
14+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
15+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
1116

1217
$registry->unregister('isSecureArea');
1318
$registry->register('isSecureArea', true);

0 commit comments

Comments
 (0)