9
9
use Magento \Catalog \Api \Data \ProductAttributeInterface ;
10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
11
use Magento \Catalog \Api \Data \ProductInterfaceFactory ;
12
+ use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
12
13
use Magento \Catalog \Api \ProductRepositoryInterface ;
13
14
use Magento \Catalog \Model \Config ;
14
15
use Magento \Catalog \Model \Product \Gallery \ReadHandler as GalleryReadHandler ;
15
16
use Magento \ConfigurableProduct \Model \Product \Type \Collection \SalableProcessor ;
16
17
use Magento \Framework \App \ObjectManager ;
17
18
use Magento \Framework \EntityManager \MetadataPool ;
19
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
18
20
19
21
/**
20
22
* Configurable product type implementation
@@ -194,9 +196,18 @@ class Configurable extends \Magento\Catalog\Model\Product\Type\AbstractType
194
196
*/
195
197
private $ salableProcessor ;
196
198
199
+ /**
200
+ * @var ProductAttributeRepositoryInterface|null
201
+ */
202
+ private $ productAttributeRepository ;
203
+
204
+ /**
205
+ * @var SearchCriteriaBuilder|null
206
+ */
207
+ private $ searchCriteriaBuilder ;
208
+
197
209
/**
198
210
* @codingStandardsIgnoreStart/End
199
- *
200
211
* @param \Magento\Catalog\Model\Product\Option $catalogProductOption
201
212
* @param \Magento\Eav\Model\Config $eavConfig
202
213
* @param \Magento\Catalog\Model\Product\Type $catalogProductType
@@ -214,9 +225,13 @@ class Configurable extends \Magento\Catalog\Model\Product\Type\AbstractType
214
225
* @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable
215
226
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
216
227
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
228
+ * @param \Magento\Framework\Cache\FrontendInterface|null $cache
229
+ * @param \Magento\Customer\Model\Session|null $customerSession
217
230
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
218
231
* @param ProductInterfaceFactory $productFactory
219
232
* @param SalableProcessor $salableProcessor
233
+ * @param ProductAttributeRepositoryInterface|null $productAttributeRepository
234
+ * @param SearchCriteriaBuilder|null $searchCriteriaBuilder
220
235
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
221
236
*/
222
237
public function __construct (
@@ -241,7 +256,9 @@ public function __construct(
241
256
\Magento \Customer \Model \Session $ customerSession = null ,
242
257
\Magento \Framework \Serialize \Serializer \Json $ serializer = null ,
243
258
ProductInterfaceFactory $ productFactory = null ,
244
- SalableProcessor $ salableProcessor = null
259
+ SalableProcessor $ salableProcessor = null ,
260
+ ProductAttributeRepositoryInterface $ productAttributeRepository = null ,
261
+ SearchCriteriaBuilder $ searchCriteriaBuilder = null
245
262
) {
246
263
$ this ->typeConfigurableFactory = $ typeConfigurableFactory ;
247
264
$ this ->_eavAttributeFactory = $ eavAttributeFactory ;
@@ -256,6 +273,10 @@ public function __construct(
256
273
$ this ->productFactory = $ productFactory ?: ObjectManager::getInstance ()
257
274
->get (ProductInterfaceFactory::class);
258
275
$ this ->salableProcessor = $ salableProcessor ?: ObjectManager::getInstance ()->get (SalableProcessor::class);
276
+ $ this ->productAttributeRepository = $ productAttributeRepository ?:
277
+ ObjectManager::getInstance ()->get (ProductAttributeRepositoryInterface::class);
278
+ $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ?:
279
+ ObjectManager::getInstance ()->get (SearchCriteriaBuilder::class);
259
280
parent ::__construct (
260
281
$ catalogProductOption ,
261
282
$ eavConfig ,
@@ -1231,19 +1252,16 @@ public function isPossibleBuyFromList($product)
1231
1252
1232
1253
/**
1233
1254
* Returns array of sub-products for specified configurable product
1234
- *
1235
- * $requiredAttributeIds - one dimensional array, if provided
1236
1255
* Result array contains all children for specified configurable product
1237
1256
*
1238
1257
* @param \Magento\Catalog\Model\Product $product
1239
- * @param array $requiredAttributeIds
1258
+ * @param array $requiredAttributeIds Attributes to include in the select; one-dimensional array
1240
1259
* @return ProductInterface[]
1241
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
1242
1260
*/
1243
1261
public function getUsedProducts ($ product , $ requiredAttributeIds = null )
1244
1262
{
1245
1263
if (!$ product ->hasData ($ this ->_usedProducts )) {
1246
- $ collection = $ this ->getConfiguredUsedProductCollection ($ product , false );
1264
+ $ collection = $ this ->getConfiguredUsedProductCollection ($ product , false , $ requiredAttributeIds );
1247
1265
$ usedProducts = array_values ($ collection ->getItems ());
1248
1266
$ product ->setData ($ this ->_usedProducts , $ usedProducts );
1249
1267
}
@@ -1390,25 +1408,38 @@ private function getUsedProductsCacheKey($keyParts)
1390
1408
1391
1409
/**
1392
1410
* Prepare collection for retrieving sub-products of specified configurable product
1393
- *
1394
1411
* Retrieve related products collection with additional configuration
1395
1412
*
1396
1413
* @param \Magento\Catalog\Model\Product $product
1397
1414
* @param bool $skipStockFilter
1415
+ * @param array $requiredAttributeIds Attributes to include in the select
1398
1416
* @return \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection
1417
+ * @throws \Magento\Framework\Exception\LocalizedException
1399
1418
*/
1400
1419
private function getConfiguredUsedProductCollection (
1401
1420
\Magento \Catalog \Model \Product $ product ,
1402
- $ skipStockFilter = true
1421
+ $ skipStockFilter = true ,
1422
+ $ requiredAttributeIds = null
1403
1423
) {
1404
1424
$ collection = $ this ->getUsedProductCollection ($ product );
1405
1425
1406
1426
if ($ skipStockFilter ) {
1407
1427
$ collection ->setFlag ('has_stock_status_filter ' , true );
1408
1428
}
1409
1429
1430
+ $ attributesForSelect = $ this ->getAttributesForCollection ($ product );
1431
+ if ($ requiredAttributeIds ) {
1432
+ $ this ->searchCriteriaBuilder ->addFilter ('attribute_id ' , $ requiredAttributeIds , 'in ' );
1433
+ $ requiredAttributes = $ this ->productAttributeRepository
1434
+ ->getList ($ this ->searchCriteriaBuilder ->create ())->getItems ();
1435
+ $ requiredAttributeCodes = [];
1436
+ foreach ($ requiredAttributes as $ requiredAttribute ) {
1437
+ $ requiredAttributeCodes [] = $ requiredAttribute ->getAttributeCode ();
1438
+ }
1439
+ $ attributesForSelect = array_unique (array_merge ($ attributesForSelect , $ requiredAttributeCodes ));
1440
+ }
1410
1441
$ collection
1411
- ->addAttributeToSelect ($ this -> getAttributesForCollection ( $ product ) )
1442
+ ->addAttributeToSelect ($ attributesForSelect )
1412
1443
->addFilterByRequiredOptions ()
1413
1444
->setStoreId ($ product ->getStoreId ());
1414
1445
0 commit comments