You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow construction of products with custom_attributes
This patch does two things:
1. Currently it is not possible to pass an array with `custom_attributes`
to the `\Magento\Catalog\Model\Product` constructor (it causes a fatal error).
The reason is because the `filterCustomAttribute` and `eavConfig` arguments are
assigned after the call to `parent::__construct`.
However, the properties are used during the `parent::__construct` calls.
The flow of execution is as follows:
Product::__construct -> Catalog\Model\AbstractModel::__construct
Catalog\Model\AbstractModel::__construct -> AbstractExtensibleModel::__construct
AbstractExtensibleModel::__construct -> AbstractExtensibleModel::filterCustomAttributes
AbstractExtensibleModel::filterCustomAttributes -> AbstractExtensibleModel::getCustomAttributesCodes
...which is overridden by Product::getCustomAttributesCodes
getCustomAttributesCodes expectes the `filterCustomAttribute` and `eavConfig` properties to be set if
`custom_attributes` are present in `$data`, but they are still null because the `Product::__construct`
method has not yet completed.
The fix this PR applies is to assign the properties before the call to `parent::__construct`.
The bug and fix are covered by the integration test:
`\Magento\Catalog\Model\ProductTest::testConstructionWithCustomAttributesMapInData`
2. The method `AbstractExtensibleModel::filterCustomAttribute` expects the `custom_attributes` in `$data` to
be a simple map from codes to values, e.g. `['category_ids => '1,2']`.
However, the method `\Magento\Framework\Reflection\DataObjectProcessor::buildOutputDataArray` generates a
numerically indexed custom attributes array, where each custom attribute is a sub-array with a `attribute_code`
and `value` record.
This PR allows passing such an `custom_attributes` array into the `Product` model constructor.
Currently it would be ignored, but with this patch the code checks if `custom_attributes` is numerically indexed,
and if so, flattens the sub-arrays into the expected map format.
To illustrate the difference of the `custom_attributes` array formats:
Map:
[
'custom_attributes' => [
'category_ids' => '1,2',
'tax_class_id' => '3',
]
]
Numerically indexed array of sub-arrays:
[
'custom_attributes' => [
[
'attribute_code' => 'category_ids',
'value' => '1,2'
],
[
'attribute_code' => 'tax_class_id',
'value' => '3'
],
]
]
This improvement is covered by the integration test
`\Magento\Catalog\Model\ProductTest::testConstructionWithCustomAttributesArrayInData`
0 commit comments