Skip to content

Commit 3877414

Browse files
author
Dmytro Voskoboinikov
committed
MAGETWO-64547: Bundle Products - The options you selected are not available
1 parent 7006528 commit 3877414

File tree

1 file changed

+143
-13
lines changed

1 file changed

+143
-13
lines changed

setup/src/Magento/Setup/Model/FixtureGenerator/BundleProductGenerator.php

Lines changed: 143 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Setup\Model\FixtureGenerator;
87

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\App\ResourceConnection;
10+
911
/**
1012
* Generate specified amount of bundle products based on passed fixture
1113
*
@@ -21,17 +23,37 @@
2123
*/
2224
class BundleProductGenerator
2325
{
26+
/**
27+
* @var array
28+
*/
29+
private $sequenceValues = [
30+
'sequence_product_bundle_option' => null,
31+
'sequence_product_bundle_selection' => null
32+
];
33+
2434
/**
2535
* @var ProductGeneratorFactory
2636
*/
2737
private $productGeneratorFactory;
2838

39+
/**
40+
* @var ResourceConnection
41+
*/
42+
private $resource;
43+
2944
/**
3045
* @param ProductGeneratorFactory $productGeneratorFactory
46+
* @param ResourceConnection $resource|null
3147
*/
32-
public function __construct(ProductGeneratorFactory $productGeneratorFactory)
33-
{
48+
public function __construct(
49+
ProductGeneratorFactory $productGeneratorFactory,
50+
ResourceConnection $resource = null
51+
) {
3452
$this->productGeneratorFactory = $productGeneratorFactory;
53+
54+
$this->resource = $resource ?: ObjectManager::getInstance()->get(
55+
ResourceConnection::class
56+
);
3557
}
3658

3759
/**
@@ -45,30 +67,90 @@ public function generate($products, $fixtureMap)
4567
{
4668
$this->productGeneratorFactory->create([
4769
'customTableMap' => [
70+
'catalog_product_bundle_option' => [
71+
'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
72+
'handler' => function ($productId, $entityNumber, $fixture, $binds) {
73+
foreach ($binds as &$bind) {
74+
$bind['option_id'] = $this->generateOptionId(
75+
$entityNumber,
76+
$bind['option_id'],
77+
$fixture
78+
);
79+
80+
$bind['parent_id'] = $productId;
81+
}
82+
83+
return $binds;
84+
},
85+
],
86+
'sequence_product_bundle_option' => [
87+
'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
88+
'handler' => function ($productId, $entityNumber, $fixture, $binds) {
89+
foreach ($binds as &$bind) {
90+
$bind['sequence_value'] = $this->generateSequenceId(
91+
'sequence_product_bundle_option'
92+
);
93+
}
94+
95+
return $binds;
96+
},
97+
],
4898
'catalog_product_bundle_option_value' => [
4999
'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
50100
'handler' => function ($productId, $entityNumber, $fixture, $binds) {
51101
foreach ($binds as &$bind) {
52-
$bind['option_id'] = $this->generateOptionId($bind['option_id'], $entityNumber, $fixture);
102+
$bind['option_id'] = $this->generateOptionId(
103+
$entityNumber,
104+
$bind['option_id'],
105+
$fixture
106+
);
107+
108+
$bind['parent_product_id'] = $productId;
53109
}
110+
54111
return $binds;
55112
},
56113
],
57114
'catalog_product_bundle_selection' => [
58115
'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
59116
'handler' => function ($productId, $entityNumber, $fixture, $binds) {
60117
foreach ($binds as &$bind) {
61-
$bind['option_id'] = $this->generateOptionId($bind['option_id'], $entityNumber, $fixture);
118+
$bind['selection_id'] = $this->generateSelectionId(
119+
$entityNumber,
120+
$bind['selection_id'],
121+
$fixture
122+
);
123+
62124
$bind['parent_product_id'] = $productId;
63-
$simpleProductId = $this->generateSimpleProductId(
125+
126+
$bind['option_id'] = $this->generateOptionId(
127+
$entityNumber,
128+
$bind['option_id'],
129+
$fixture
130+
);
131+
132+
$bind['product_id'] = $this->generateSimpleProductId(
64133
$bind['product_id'],
65134
$entityNumber,
66135
$fixture
67136
);
68-
$bind['product_id'] = $simpleProductId;
69-
$bind['selection_price_value'] = $fixture['price']($simpleProductId);
70-
$bind['selection_price_type'] = $fixture['priceType']($simpleProductId);
137+
138+
$bind['selection_price_type'] = $fixture['priceType']($bind['product_id']);
139+
$bind['selection_price_value'] = $fixture['price']($bind['product_id']);
140+
}
141+
142+
return $binds;
143+
},
144+
],
145+
'sequence_product_bundle_selection' => [
146+
'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
147+
'handler' => function ($productId, $entityNumber, $fixture, $binds) {
148+
foreach ($binds as &$bind) {
149+
$bind['sequence_value'] = $this->generateSequenceId(
150+
'sequence_product_bundle_selection'
151+
);
71152
}
153+
72154
return $binds;
73155
},
74156
],
@@ -91,16 +173,64 @@ public function generate($products, $fixtureMap)
91173
}
92174

93175
/**
94-
* Generate value of option_id for $entityNumber bundle product based on previous option_id
176+
* Generates an option Id.
177+
*
178+
* @param int $entityNumber
179+
* @param int $originalOptionId
180+
* @param array $fixture
181+
*
182+
* @return int|null
183+
*/
184+
private function generateOptionId($entityNumber, $originalOptionId, array $fixture)
185+
{
186+
if ($originalOptionId) {
187+
return $fixture['_bundle_options'] * ($entityNumber + 1) + $originalOptionId;
188+
}
189+
190+
return $originalOptionId;
191+
}
192+
193+
/**
194+
* Generates a selection Id.
95195
*
96-
* @param int $previousOptionId
97196
* @param int $entityNumber
197+
* @param int $originalSelectionId
98198
* @param array $fixture
199+
*
200+
* @return int|null
201+
*/
202+
private function generateSelectionId($entityNumber, $originalSelectionId, array $fixture)
203+
{
204+
if ($originalSelectionId) {
205+
$selectionsPerProduct = $fixture['_bundle_products_per_option'] * $fixture['_bundle_options'];
206+
207+
return $selectionsPerProduct * ($entityNumber + 1) + $originalSelectionId;
208+
}
209+
210+
return $originalSelectionId;
211+
}
212+
213+
/**
214+
* Generates an Id for the given sequence table.
215+
*
216+
* @param string $tableName
217+
*
99218
* @return int
100219
*/
101-
private function generateOptionId($previousOptionId, $entityNumber, array $fixture)
220+
private function generateSequenceId($tableName)
102221
{
103-
return $previousOptionId + $entityNumber * $fixture['_bundle_options'] + $fixture['_bundle_options'];
222+
if (!$this->sequenceValues[$tableName]) {
223+
$connection = $this->resource->getConnection();
224+
225+
$this->sequenceValues[$tableName] = $connection->fetchOne(
226+
$connection->select()->from(
227+
$connection->getTableName($tableName),
228+
'MAX(`sequence_value`)'
229+
)
230+
);
231+
}
232+
233+
return ++$this->sequenceValues[$tableName];
104234
}
105235

106236
/**

0 commit comments

Comments
 (0)