Skip to content

Commit

Permalink
Merge pull request #559 from magento-performance/MAGETWO-55791
Browse files Browse the repository at this point in the history
[Performance] Fast Display and Generation of Product Variations
- MAGETWO-55300 [Customer] Fast Display and Generation of Product Variations for Configurable Products
  • Loading branch information
slavvka authored Nov 2, 2016
2 parents 90e0f4c + 87274f3 commit 0d31c3e
Show file tree
Hide file tree
Showing 15 changed files with 690 additions and 158 deletions.
24 changes: 24 additions & 0 deletions app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function execute()
$productTypeId = $this->getRequest()->getParam('type');
if ($data) {
try {
$data = $this->unserializeData($data);
$product = $this->initializationHelper->initialize($this->productBuilder->build($this->getRequest()));
$this->productTypeManager->processProduct($product);

Expand Down Expand Up @@ -157,6 +158,29 @@ public function execute()
return $resultRedirect;
}

/**
* @param array $data
* @return array
*/
private function unserializeData($data)
{
if (isset($data["configurable-matrix-serialized"])) {
$configurableMatrixSerialized = $data["configurable-matrix-serialized"];
if ($configurableMatrixSerialized != null && !empty($configurableMatrixSerialized)) {
$data["variations-matrix"] = json_decode($configurableMatrixSerialized, true);
unset($data["configurable-matrix-serialized"]);
}
}
if (isset($data["associated_product_ids_serialized"])) {
$associatedProductIdsSerialized = $data["associated_product_ids_serialized"];
if ($associatedProductIdsSerialized != null && !empty($associatedProductIdsSerialized)) {
$data["associated_product_ids"] = json_decode($associatedProductIdsSerialized, true);
unset($data["associated_product_ids_serialized"]);
}
}
return $data;
}

/**
* Notify customer when image was not deleted in specific case.
* TODO: temporary workaround must be eliminated in MAGETWO-45306
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ public function afterInitialize(
$this->productType->setUsedProductAttributeIds($attributes, $product);

$product->setNewVariationsAttributeSetId($setId);
$associatedProductIds = $this->request->getPost('associated_product_ids', []);
$variationsMatrix = $this->request->getParam('variations-matrix', []);
$associatedProductIds = $this->request->getPost('associated_product_ids_serialized', '[]');
if ($associatedProductIds !== null && !empty($associatedProductIds)) {
$associatedProductIds = json_decode($associatedProductIds, true);
}
$variationsMatrix = $this->request->getParam('configurable-matrix-serialized', '[]');
if ($variationsMatrix !== null && !empty($variationsMatrix)) {
$variationsMatrix = json_decode($variationsMatrix, true);
}
if (!empty($variationsMatrix)) {
$generatedProductIds = $this->variationHandler->generateSimpleProducts($product, $variationsMatrix);
$associatedProductIds = array_merge($associatedProductIds, $generatedProductIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function afterInitialize(
\Magento\Catalog\Model\Product $configurableProduct
) {
$configurations = $this->request->getParam('configurations', []);
if ($this->request->getParam('configurations_serialized')) {
$configurations = json_decode($this->request->getParam('configurations_serialized'), true);
}
$configurations = $this->variationHandler->duplicateImagesForVariations($configurations);
foreach ($configurations as $productId => $productData) {
/** @var \Magento\Catalog\Model\Product $product */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function aroundValidate(
$product->setTypeId(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE);
}
$result = $proceed($product, $request, $response);
$variationProducts = (array)$request->getPost('variations-matrix');
$variationProducts = $request->getPost('configurable-matrix-serialized', '[]');
$variationProducts = json_decode($variationProducts, true);
if ($variationProducts) {
$validationResult = $this->_validateProductVariations($product, $variationProducts, $request);
if (!empty($validationResult)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,23 @@ public function testAfterInitializeIfAttributesNotEmptyAndActionNameNotGenerateV
{
$this->productMock->expects($this->once())->method('getTypeId')->willReturn(ConfigurableProduct::TYPE_CODE);
$associatedProductIds = ['key' => 'value'];
$associatedProductIdsSerialized = json_encode($associatedProductIds);
$generatedProductIds = ['key_one' => 'value_one'];
$expectedArray = ['key' => 'value', 'key_one' => 'value_one'];
$attributes = ['key' => 'value'];
$postValue = 'postValue';
$variationsMatrix = ['variationKey' => 'variationValue'];
$variationsMatrixSerialized = json_encode($variationsMatrix);

$postValueMap = [
['new-variations-attribute-set-id', null, $postValue],
['associated_product_ids', [], $associatedProductIds],
['associated_product_ids_serialized', '[]', $associatedProductIdsSerialized],
['affect_configurable_product_attributes', null, $postValue],
];
$this->requestMock->expects($this->any())->method('getPost')->will($this->returnValueMap($postValueMap));

$paramValueMap = [
['variations-matrix', [], $postValue],
['configurable-matrix-serialized', '[]', $variationsMatrixSerialized],
['attributes', null, $attributes],
];
$this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($paramValueMap));
Expand All @@ -110,7 +114,7 @@ public function testAfterInitializeIfAttributesNotEmptyAndActionNameNotGenerateV
'generateSimpleProducts'
)->with(
$this->productMock,
$postValue
$variationsMatrix
)->will(
$this->returnValue($generatedProductIds)
);
Expand All @@ -123,16 +127,17 @@ public function testAfterInitializeIfAttributesNotEmptyAndActionNameGenerateVari
{
$this->productMock->expects($this->once())->method('getTypeId')->willReturn(ConfigurableProduct::TYPE_CODE);
$associatedProductIds = ['key' => 'value'];
$associatedProductIdsSerialized = json_encode($associatedProductIds);
$attributes = ['key' => 'value'];
$postValue = 'postValue';
$valueMap = [
['new-variations-attribute-set-id', null, $postValue],
['associated_product_ids', [], $associatedProductIds],
['associated_product_ids_serialized', '[]', $associatedProductIdsSerialized],
['affect_configurable_product_attributes', null, $postValue],
];
$this->requestMock->expects($this->any())->method('getPost')->will($this->returnValueMap($valueMap));
$paramValueMap = [
['variations-matrix', [], []],
['variations-matrix', '[]', '[]'],
['attributes', null, $attributes],
];
$this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap($paramValueMap));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected function setUp()

public function testAroundValidateWithVariationsValid()
{
$matrix = ['products'];
$matrix = json_encode(['products']);

$plugin = $this->getMock(
'Magento\ConfigurableProduct\Model\Product\Validator\Plugin',
Expand All @@ -124,7 +124,7 @@ public function testAroundValidateWithVariationsValid()
'_validateProductVariations'
)->with(
$this->productMock,
$matrix,
json_decode($matrix),
$this->requestMock
)->will(
$this->returnValue(null)
Expand All @@ -135,7 +135,8 @@ public function testAroundValidateWithVariationsValid()
)->method(
'getPost'
)->with(
'variations-matrix'
'configurable-matrix-serialized',
'[]'
)->will(
$this->returnValue($matrix)
);
Expand All @@ -156,7 +157,7 @@ public function testAroundValidateWithVariationsValid()

public function testAroundValidateWithVariationsInvalid()
{
$matrix = ['products'];
$matrix = json_encode(['products']);

$plugin = $this->getMock(
'Magento\ConfigurableProduct\Model\Product\Validator\Plugin',
Expand All @@ -170,7 +171,7 @@ public function testAroundValidateWithVariationsInvalid()
'_validateProductVariations'
)->with(
$this->productMock,
$matrix,
json_decode($matrix),
$this->requestMock
)->will(
$this->returnValue(true)
Expand All @@ -181,7 +182,8 @@ public function testAroundValidateWithVariationsInvalid()
)->method(
'getPost'
)->with(
'variations-matrix'
'configurable-matrix-serialized',
'[]'
)->will(
$this->returnValue($matrix)
);
Expand All @@ -208,9 +210,10 @@ public function testAroundValidateIfVariationsNotExist()
)->method(
'getPost'
)->with(
'variations-matrix'
'configurable-matrix-serialized',
'[]'
)->will(
$this->returnValue(null)
$this->returnValue('[]')
);
$this->eventManagerMock->expects($this->never())->method('dispatch');
$this->plugin->aroundValidate(
Expand All @@ -225,10 +228,11 @@ public function testAroundValidateIfVariationsNotExist()
public function testAroundValidateWithVariationsAndRequiredAttributes()
{
$matrix = [
['data1', 'data2', 'configurable_attribute' => ['data1']],
['data3', 'data4', 'configurable_attribute' => ['data3']],
['data5', 'data6', 'configurable_attribute' => ['data5']],
['data1', 'data2', 'configurable_attribute' => ['data1'], 'price' => 10],
['data3', 'data4', 'configurable_attribute' => ['data3'], 'price' => 10],
['data5', 'data6', 'configurable_attribute' => ['data5'], 'price' => 10],
];
$encodedMatrix = json_encode($matrix);

$this->productMock->expects($this->any())
->method('getData')
Expand All @@ -249,9 +253,10 @@ public function testAroundValidateWithVariationsAndRequiredAttributes()
)->method(
'getPost'
)->with(
'variations-matrix'
'configurable-matrix-serialized',
'[]'
)->will(
$this->returnValue($matrix)
$this->returnValue($encodedMatrix)
);

$attribute1 = $this->createAttribute('code1', true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,33 @@

<div class="admin__data-grid-wrap admin__data-grid-wrap-static">
<!-- ko if: gridNew().length -->
<!-- ko template: {name: getGridTemplate(), data: {
grid: gridNew,
id: getGridId(),
title: $t('New Product Review'),
note: $t('Here are the products you\'re about to create.')
}} --><!-- /ko -->
<!-- ko template: {name: getGridTemplate(), data: {
grid: gridNew,
paging: pagingNew,
id: getGridId(),
title: $t('New Product Review'),
note: $t('Here are the products you\'re about to create.')
}} --><!-- /ko -->
<!-- /ko -->

<!-- ko if: gridExisting().length -->
<!-- ko template: {name: getGridTemplate(), data: {
grid: gridExisting,
id: getGridId(),
title: $t('Associated Products'),
note: $t('You created these products for this configuration.')
}} --><!-- /ko -->
<!-- ko template: {name: getGridTemplate(), data: {
paging: pagingExisting,
grid: gridExisting,
id: getGridId(),
title: $t('Associated Products'),
note: $t('You created these products for this configuration.')
}} --><!-- /ko -->
<!-- /ko -->

<!-- ko if: gridDeleted().length -->
<!-- ko template: {name: getGridTemplate(), data: {
grid: gridDeleted,
id: getGridId(),
title: $t('Disassociated Products'),
note: $t('These products are not associated.')
}} --><!-- /ko -->
<!-- ko template: {name: getGridTemplate(), data: {
paging: pagingDeleted,
grid: gridDeleted,
id: getGridId(),
title: $t('Disassociated Products'),
note: $t('These products are not associated.')
}} --><!-- /ko -->
<!-- /ko -->
</div>
</div>
Expand Down
Loading

0 comments on commit 0d31c3e

Please sign in to comment.