-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Allow construction of products with custom_attributes in $data #24460
Allow construction of products with custom_attributes in $data #24460
Conversation
Hi @Vinai. Thank you for your contribution
For more details, please, review the Magento Contributor Guide documentation. |
}, | ||
$websiteIds | ||
); | ||
$storeIds = array_merge($storeIds, ...$websiteStoreIds); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to reviewers: This change is only in the PR because the static tests complained about the array_merge
in a loop. I didn't write that loop, but I don't mind changing it to get the tests green.
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`
3b4239f
to
cd3b244
Compare
Resolved merge conflicts. |
I do not know why the Database Compare check fails, as the PR does not include any storage related changes. Can someone with more insight help, please? |
Hi @orlangur, thank you for the review. |
Hi @sidolov, thank you for the review. |
@engcom-Foxtrot , please, take a look at the failed test on the Magento Commerce and B2B edition: |
@magento run all tests |
1 similar comment
@magento run all tests |
# Conflicts: # app/code/Magento/Catalog/Model/Product.php # dev/tests/integration/testsuite/Magento/Catalog/Model/ProductTest.php
105e4a1
to
7139588
Compare
7139588
to
f095f73
Compare
@magento run all tests |
Internal BIC approval ticket https://jira.corp.magento.com/browse/MC-30325 |
BIC ia approved |
✔️ QA Passed |
Hi @Vinai, thank you for your contribution! |
This is a new PR because I accidentally deleted the previous remote branch for #24407
Description (*)
This patch does two things:
custom_attributes
to the
\Magento\Catalog\Model\Product
constructor (it causes a fatal error).The reason is because the
filterCustomAttribute
andeavConfig
arguments areassigned 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
expects thefilterCustomAttribute
andeavConfig
properties to be set ifcustom_attributes
are present in$data
, but they are stillnull
because theProduct::__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
AbstractExtensibleModel::filterCustomAttribute
expects thecustom_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 anumerically indexed custom attributes array, where each custom attribute is a sub-array with a
attribute_code
andvalue
record.This PR allows passing such an
custom_attributes
array into theProduct
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:This improvement is covered by the integration test
\Magento\Catalog\Model\ProductTest::testConstructionWithCustomAttributesArrayInData
Description (*)
Manual testing scenarios (*)
Magento\Catalog\Model\Product
and pass a$data
array with acustom_attributes
key to the constructor. Thecustom_attributes
array can be a map or a numerically indexed array, the exception is thrown either way.Examples of how this can be done can be seen in the integration tests in this PR.
Contribution checklist (*)