Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13254 configurable products are not saving with multiple associate products #17992

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,7 +13,9 @@
use Magento\ConfigurableProduct\Helper\Product\Options\Factory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableProduct;
use Magento\ConfigurableProduct\Model\Product\VariationHandler;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;

/**
* Class Configurable
Expand All @@ -37,6 +39,11 @@ class Configurable
*/
private $optionsFactory;

/**
* @var JsonSerializer
*/
private $serializer;

/**
* @var array
*/
Expand Down Expand Up @@ -64,11 +71,13 @@ class Configurable
public function __construct(
VariationHandler $variationHandler,
RequestInterface $request,
Factory $optionsFactory
Factory $optionsFactory,
JsonSerializer $serializer = null
) {
$this->variationHandler = $variationHandler;
$this->request = $request;
$this->optionsFactory = $optionsFactory;
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(JsonSerializer::class);
}

/**
Expand Down Expand Up @@ -99,9 +108,9 @@ public function afterInitialize(Helper $subject, ProductInterface $product)
$product->setNewVariationsAttributeSetId($setId);

$configurableOptions = [];
if (!empty($productData['configurable_attributes_data'])) {
if (!empty($productData['configurable_attributes_data_serialized'])) {
$configurableOptions = $this->optionsFactory->create(
(array) $productData['configurable_attributes_data']
$this->serializer->unserialize($productData['configurable_attributes_data_serialized'])
);
}

Expand All @@ -128,8 +137,8 @@ public function afterInitialize(Helper $subject, ProductInterface $product)
private function setLinkedProducts(ProductInterface $product, ProductExtensionInterface $extensionAttributes)
{
$associatedProductIds = $this->request->getPost('associated_product_ids_serialized', '[]');
if ($associatedProductIds != null && !empty($associatedProductIds)) {
$associatedProductIds = json_decode($associatedProductIds, true);
if (!empty($associatedProductIds)) {
$associatedProductIds = $this->serializer->unserialize($associatedProductIds);
}

$variationsMatrix = $this->getVariationMatrix();
Expand All @@ -155,7 +164,7 @@ protected function getVariationMatrix()
$result = [];
$configurableMatrix = $this->request->getParam('configurable-matrix-serialized', "[]");
if (isset($configurableMatrix) && $configurableMatrix != "") {
$configurableMatrix = json_decode($configurableMatrix, true);
$configurableMatrix = $this->serializer->unserialize($configurableMatrix);

foreach ($configurableMatrix as $item) {
if ($item['newProduct']) {
Expand Down
Expand Up @@ -13,6 +13,8 @@
use Magento\ConfigurableProduct\Model\Product\VariationHandler;
use Magento\ConfigurableProduct\Test\Unit\Model\Product\ProductExtensionAttributes;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
Expand Down Expand Up @@ -50,6 +52,16 @@ class ConfigurableTest extends \PHPUnit\Framework\TestCase
*/
private $plugin;

/**
* @var JsonSerializer
*/
private $serializer;

/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -87,6 +99,9 @@ protected function setUp()
$this->request,
$this->optionFactory
);

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->serializer = $this->objectManagerHelper->getObject(JsonSerializer::class);
}

/**
Expand All @@ -102,7 +117,7 @@ public function testAfterInitializeWithAttributesAndVariations()
$valueMap = [
['new-variations-attribute-set-id', null, 24],
['associated_product_ids_serialized', '[]', []],
['product', [], ['configurable_attributes_data' => $attributes]],
['product', [], ['configurable_attributes_data_serialized' => $this->serializer->serialize($attributes)]],
];
$simpleProductsIds = [1, 2, 3];
$simpleProducts = [
Expand Down Expand Up @@ -150,7 +165,7 @@ public function testAfterInitializeWithAttributesAndVariations()
]
];
$paramValueMap = [
['configurable-matrix-serialized', "[]", json_encode($simpleProducts)],
['configurable-matrix-serialized', "[]", $this->serializer->serialize($simpleProducts)],
['attributes', null, $attributes],
];

Expand Down Expand Up @@ -213,7 +228,7 @@ public function testAfterInitializeWithAttributesAndWithoutVariations()
$valueMap = [
['new-variations-attribute-set-id', null, 24],
['associated_product_ids_serialized', "[]", "[]"],
['product', [], ['configurable_attributes_data' => $attributes]],
['product', [], ['configurable_attributes_data_serialized' => $this->serializer->serialize($attributes)]],
];
$paramValueMap = [
['configurable-matrix-serialized', "[]", "[]"],
Expand Down
Expand Up @@ -22,6 +22,7 @@ define([
this.message = message;
this.name = 'UserException';
}

UserException.prototype = Object.create(Error.prototype);

return Component.extend({
Expand Down Expand Up @@ -193,12 +194,12 @@ define([

_.each(variations, function (variation) {
var attributes = _.reduce(variation.options, function (memo, option) {
var attribute = {};
var attribute = {};

attribute[option['attribute_code']] = option.value;
attribute[option['attribute_code']] = option.value;

return _.extend(memo, attribute);
}, {}),
return _.extend(memo, attribute);
}, {}),
gallery = {
images: {}
},
Expand Down Expand Up @@ -418,6 +419,12 @@ define([
JSON.stringify(this.source.data['associated_product_ids']);
delete this.source.data['associated_product_ids'];
}

if (this.source.data.product['configurable_attributes_data']) {
this.source.data.product['configurable_attributes_data_serialized'] =
JSON.stringify(this.source.data.product['configurable_attributes_data']);
delete this.source.data.product['configurable_attributes_data'];
}
},

/**
Expand Down