Skip to content

Commit 3beb931

Browse files
committed
Merge pull request #391 from magento-mpi/MAGETWO-47395
[MPI] Configurable Product Contribution
2 parents 02b9466 + 6f634d2 commit 3beb931

File tree

52 files changed

+3006
-1690
lines changed

Some content is hidden

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

52 files changed

+3006
-1690
lines changed

app/code/Magento/Catalog/Model/Indexer/Product/Flat/AbstractAction.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Product\Flat;
77

8+
use Magento\Catalog\Api\Data\ProductInterface;
89
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\Model\Entity\MetadataPool;
911

1012
/**
1113
* Abstract action reindex class
12-
*
14+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1315
*/
1416
abstract class AbstractAction
1517
{
@@ -72,6 +74,12 @@ abstract class AbstractAction
7274
protected $_flatTableBuilder;
7375

7476
/**
77+
* @var MetadataPool
78+
*/
79+
private $metadataPool;
80+
81+
/**
82+
* @param MetadataPool $metadataPool
7583
* @param \Magento\Framework\App\ResourceConnection $resource
7684
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
7785
* @param \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper
@@ -80,6 +88,7 @@ abstract class AbstractAction
8088
* @param FlatTableBuilder $flatTableBuilder
8189
*/
8290
public function __construct(
91+
MetadataPool $metadataPool,
8392
\Magento\Framework\App\ResourceConnection $resource,
8493
\Magento\Store\Model\StoreManagerInterface $storeManager,
8594
\Magento\Catalog\Helper\Product\Flat\Indexer $productHelper,
@@ -93,6 +102,7 @@ public function __construct(
93102
$this->_connection = $resource->getConnection();
94103
$this->_tableBuilder = $tableBuilder;
95104
$this->_flatTableBuilder = $flatTableBuilder;
105+
$this->metadataPool = $metadataPool;
96106
}
97107

98108
/**
@@ -194,6 +204,8 @@ protected function _updateRelationProducts($storeId, $productIds = null)
194204
return $this;
195205
}
196206

207+
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
208+
197209
foreach ($this->_getProductTypeInstances() as $typeInstance) {
198210
/** @var $typeInstance \Magento\Catalog\Model\Product\Type\AbstractType */
199211
if (!$typeInstance->isComposite(null)) {
@@ -210,7 +222,11 @@ protected function _updateRelationProducts($storeId, $productIds = null)
210222
/** @var $select \Magento\Framework\DB\Select */
211223
$select = $this->_connection->select()->from(
212224
['t' => $this->_productIndexerHelper->getTable($relation->getTable())],
213-
[$relation->getParentFieldName(), $relation->getChildFieldName(), new \Zend_Db_Expr('1')]
225+
[$relation->getChildFieldName(), new \Zend_Db_Expr('1')]
226+
)->join(
227+
['entity_table' => $this->_connection->getTableName('catalog_product_entity')],
228+
'entity_table.' . $metadata->getLinkField() . 't.' . $relation->getParentFieldName(),
229+
[$relation->getParentFieldName() => 'entity_table.entity_id']
214230
)->join(
215231
['e' => $this->_productIndexerHelper->getFlatTableName($storeId)],
216232
"e.entity_id = t.{$relation->getChildFieldName()}",
@@ -222,7 +238,7 @@ protected function _updateRelationProducts($storeId, $productIds = null)
222238
if ($productIds !== null) {
223239
$cond = [
224240
$this->_connection->quoteInto("{$relation->getChildFieldName()} IN(?)", $productIds),
225-
$this->_connection->quoteInto("{$relation->getParentFieldName()} IN(?)", $productIds),
241+
$this->_connection->quoteInto("entity_table.entity_id IN(?)", $productIds),
226242
];
227243

228244
$select->where(implode(' OR ', $cond));
@@ -247,6 +263,8 @@ protected function _cleanRelationProducts($storeId)
247263
return $this;
248264
}
249265

266+
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
267+
250268
foreach ($this->_getProductTypeInstances() as $typeInstance) {
251269
/** @var $typeInstance \Magento\Catalog\Model\Product\Type\AbstractType */
252270
if (!$typeInstance->isComposite(null)) {
@@ -258,11 +276,15 @@ protected function _cleanRelationProducts($storeId)
258276
$select = $this->_connection->select()->distinct(
259277
true
260278
)->from(
261-
$this->_productIndexerHelper->getTable($relation->getTable()),
262-
"{$relation->getParentFieldName()}"
279+
['t' => $this->_productIndexerHelper->getTable($relation->getTable())],
280+
[]
281+
)->join(
282+
['entity_table' => $this->_connection->getTableName('catalog_product_entity')],
283+
'entity_table.' . $metadata->getLinkField() . 't.' . $relation->getParentFieldName(),
284+
[$relation->getParentFieldName() => 'entity_table.entity_id']
263285
);
264286
$joinLeftCond = [
265-
"e.entity_id = t.{$relation->getParentFieldName()}",
287+
"e.entity_id = entity_table.entity_id",
266288
"e.child_id = t.{$relation->getChildFieldName()}",
267289
];
268290
if ($relation->getWhere() !== null) {

app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Row.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder;
99
use Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder;
10+
use Magento\Framework\Model\Entity\MetadataPool;
1011

1112
/**
1213
* Class Row reindex action
@@ -24,6 +25,7 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
2425
protected $flatItemEraser;
2526

2627
/**
28+
* @param MetadataPool $metadataPool
2729
* @param \Magento\Framework\App\ResourceConnection $resource
2830
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
2931
* @param \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper
@@ -34,6 +36,7 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
3436
* @param Eraser $flatItemEraser
3537
*/
3638
public function __construct(
39+
MetadataPool $metadataPool,
3740
\Magento\Framework\App\ResourceConnection $resource,
3841
\Magento\Store\Model\StoreManagerInterface $storeManager,
3942
\Magento\Catalog\Helper\Product\Flat\Indexer $productHelper,
@@ -44,6 +47,7 @@ public function __construct(
4447
Eraser $flatItemEraser
4548
) {
4649
parent::__construct(
50+
$metadataPool,
4751
$resource,
4852
$storeManager,
4953
$productHelper,

app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Rows.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder;
99
use Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder;
10+
use Magento\Framework\Model\Entity\MetadataPool;
1011

1112
/**
1213
* Class Rows reindex action for mass actions
@@ -20,6 +21,7 @@ class Rows extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
2021
protected $flatItemEraser;
2122

2223
/**
24+
* @param MetadataPool $metadataPool
2325
* @param \Magento\Framework\App\ResourceConnection $resource
2426
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
2527
* @param \Magento\Catalog\Helper\Product\Flat\Indexer $productHelper
@@ -29,6 +31,7 @@ class Rows extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
2931
* @param Eraser $flatItemEraser
3032
*/
3133
public function __construct(
34+
MetadataPool $metadataPool,
3235
\Magento\Framework\App\ResourceConnection $resource,
3336
\Magento\Store\Model\StoreManagerInterface $storeManager,
3437
\Magento\Catalog\Helper\Product\Flat\Indexer $productHelper,
@@ -38,6 +41,7 @@ public function __construct(
3841
Eraser $flatItemEraser
3942
) {
4043
parent::__construct(
44+
$metadataPool,
4145
$resource,
4246
$storeManager,
4347
$productHelper,

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Action/DataProvider.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -372,21 +372,13 @@ public function getProductChildIds($productId, $typeId)
372372
['main' => $this->getTable($relation->getTable())],
373373
[$relation->getChildFieldName()]
374374
);
375-
//TODO: Will be removed in MAGETWO-47395
376-
if ($typeId === 'configurable') {
377-
$select->where(
378-
$relation->getParentFieldName() . ' = ?',
379-
$productId
380-
);
381-
} else {
382-
$select->join(
383-
['e' => $this->resource->getTableName('catalog_product_entity')],
384-
'e.' . $this->metadata->getLinkField() . ' = main.' . $relation->getParentFieldName()
385-
)->where(
386-
'e.entity_id = ?',
387-
$productId
388-
);
389-
}
375+
$select->join(
376+
['e' => $this->resource->getTableName('catalog_product_entity')],
377+
'e.' . $this->metadata->getLinkField() . ' = main.' . $relation->getParentFieldName()
378+
)->where(
379+
'e.entity_id = ?',
380+
$productId
381+
);
390382

391383
if ($relation->getWhere() !== null) {
392384
$select->where($relation->getWhere());

app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/Tab/Variations/Config/Matrix.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ protected function prepareVariations()
303303
];
304304
foreach ($attribute->getOptions() as $option) {
305305
if (!empty($option->getValue())) {
306-
$attributes[$attribute->getAttributeId()]['options'][$option->getValue()] = [
306+
$attributes[$attribute->getAttributeId()]['options'][] = [
307307
'attribute_code' => $attribute->getAttributeCode(),
308308
'attribute_label' => $attribute->getStoreLabel(0),
309309
'id' => $option->getValue(),
@@ -322,7 +322,7 @@ protected function prepareVariations()
322322
'value' => $optionId,
323323
];
324324
$variationOptions[] = $variationOption;
325-
$attributes[$attribute->getAttributeId()]['chosen'][$optionId] = $variationOption;
325+
$attributes[$attribute->getAttributeId()]['chosen'][] = $variationOption;
326326
}
327327

328328
$productMatrix[] = [

app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Builder/Plugin.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function aroundBuild(
5151
$attributes = $request->getParam('attributes');
5252
if (!empty($attributes)) {
5353
$product->setTypeId(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE);
54-
$this->configurableType->setUsedProductAttributeIds($attributes, $product);
54+
$this->configurableType->setUsedProductAttributes($product, $attributes);
5555
} else {
5656
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
5757
}
@@ -73,7 +73,9 @@ public function aroundBuild(
7373
&& $request->getParam('id', false) === false
7474
) {
7575
$configProduct = $this->productFactory->create();
76-
$configProduct->setStoreId(0)->load($request->getParam('product'))->setTypeId($request->getParam('type'));
76+
$configProduct->setStoreId(0)
77+
->load($request->getParam('product'))
78+
->setTypeId($request->getParam('type'));
7779

7880
$data = [];
7981
foreach ($configProduct->getTypeInstance()->getEditableAttributes($configProduct) as $attribute) {
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,121 @@
11
<?php
22
/**
3-
* Product initialzation helper
3+
* Product initialization helper
44
*
55
* Copyright © 2015 Magento. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
88
namespace Magento\ConfigurableProduct\Controller\Adminhtml\Product\Initialization\Helper\Plugin;
99

10+
use Magento\Catalog\Api\Data\ProductExtensionInterface;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper;
13+
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
1014
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableProduct;
15+
use Magento\ConfigurableProduct\Model\Product\VariationHandler;
16+
use Magento\Framework\App\RequestInterface;
1117

18+
/**
19+
* Class Configurable
20+
*
21+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22+
*/
1223
class Configurable
1324
{
14-
/** @var \Magento\ConfigurableProduct\Model\Product\VariationHandler */
15-
protected $variationHandler;
25+
/**
26+
* @var VariationHandler
27+
*/
28+
private $variationHandler;
1629

17-
/** @var \Magento\Framework\App\RequestInterface */
18-
protected $request;
30+
/**
31+
* @var RequestInterface
32+
*/
33+
private $request;
1934

20-
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable */
21-
protected $productType;
35+
/**
36+
* @var Factory
37+
*/
38+
private $optionsFactory;
2239

2340
/**
24-
* @param \Magento\ConfigurableProduct\Model\Product\VariationHandler $variationHandler
25-
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productType
26-
* @param \Magento\Framework\App\RequestInterface $request
41+
* Constructor
42+
*
43+
* @param VariationHandler $variationHandler
44+
* @param RequestInterface $request
45+
* @param Factory $optionsFactory
2746
*/
2847
public function __construct(
29-
\Magento\ConfigurableProduct\Model\Product\VariationHandler $variationHandler,
30-
\Magento\ConfigurableProduct\Model\Product\Type\Configurable $productType,
31-
\Magento\Framework\App\RequestInterface $request
48+
VariationHandler $variationHandler,
49+
RequestInterface $request,
50+
Factory $optionsFactory
3251
) {
3352
$this->variationHandler = $variationHandler;
34-
$this->productType = $productType;
3553
$this->request = $request;
54+
$this->optionsFactory = $optionsFactory;
3655
}
3756

3857
/**
3958
* Initialize data for configurable product
4059
*
41-
* @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject
42-
* @param \Magento\Catalog\Model\Product $product
60+
* @param Helper $subject
61+
* @param ProductInterface $product
62+
* @return ProductInterface
63+
* @throws \InvalidArgumentException
4364
*
44-
* @return \Magento\Catalog\Model\Product
4565
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4666
*/
47-
public function afterInitialize(
48-
\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject,
49-
\Magento\Catalog\Model\Product $product
50-
) {
67+
public function afterInitialize(Helper $subject, ProductInterface $product)
68+
{
5169
$attributes = $this->request->getParam('attributes');
52-
if ($product->getTypeId() == ConfigurableProduct::TYPE_CODE && !empty($attributes)) {
53-
$setId = $this->request->getPost('new-variations-attribute-set-id');
54-
if ($setId) {
55-
$product->setAttributeSetId($setId);
56-
}
57-
$this->productType->setUsedProductAttributeIds($attributes, $product);
70+
$productData = $this->request->getPost('product', []);
71+
72+
if ($product->getTypeId() !== ConfigurableProduct::TYPE_CODE || empty($attributes)) {
73+
return $product;
74+
}
75+
76+
$setId = $this->request->getPost('new-variations-attribute-set-id');
77+
if ($setId) {
78+
$product->setAttributeSetId($setId);
79+
}
80+
$extensionAttributes = $product->getExtensionAttributes();
5881

59-
$product->setNewVariationsAttributeSetId($setId);
60-
$associatedProductIds = $this->request->getPost('associated_product_ids', []);
61-
$variationsMatrix = $this->request->getParam('variations-matrix', []);
62-
if (!empty($variationsMatrix)) {
63-
$generatedProductIds = $this->variationHandler->generateSimpleProducts($product, $variationsMatrix);
64-
$associatedProductIds = array_merge($associatedProductIds, $generatedProductIds);
65-
}
66-
$product->setAssociatedProductIds(array_filter($associatedProductIds));
82+
$product->setNewVariationsAttributeSetId($setId);
6783

68-
$product->setCanSaveConfigurableAttributes(
69-
(bool)$this->request->getPost('affect_configurable_product_attributes')
84+
$configurableOptions = [];
85+
if (!empty($productData['configurable_attributes_data'])) {
86+
$configurableOptions = $this->optionsFactory->create(
87+
(array) $productData['configurable_attributes_data']
7088
);
7189
}
7290

91+
$extensionAttributes->setConfigurableProductOptions($configurableOptions);
92+
93+
$this->setLinkedProducts($product, $extensionAttributes);
94+
$product->setCanSaveConfigurableAttributes(
95+
(bool) $this->request->getPost('affect_configurable_product_attributes')
96+
);
97+
98+
$product->setExtensionAttributes($extensionAttributes);
99+
73100
return $product;
74101
}
102+
103+
/**
104+
* Relate simple products to configurable
105+
*
106+
* @param ProductInterface $product
107+
* @param ProductExtensionInterface $extensionAttributes
108+
* @return void
109+
* @throws \Magento\Framework\Exception\LocalizedException
110+
*/
111+
private function setLinkedProducts(ProductInterface $product, ProductExtensionInterface $extensionAttributes)
112+
{
113+
$associatedProductIds = $this->request->getPost('associated_product_ids', []);
114+
$variationsMatrix = $this->request->getParam('variations-matrix', []);
115+
if (!empty($variationsMatrix)) {
116+
$generatedProductIds = $this->variationHandler->generateSimpleProducts($product, $variationsMatrix);
117+
$associatedProductIds = array_merge($associatedProductIds, $generatedProductIds);
118+
}
119+
$extensionAttributes->setConfigurableProductLinks(array_filter($associatedProductIds));
120+
}
75121
}

0 commit comments

Comments
 (0)