magento2-module-custom-entity-product-link
magento2-module-custom-entity-product-link copied to clipboard
Custom entity lost in case it's assigned to multiple products on the same page by different attributes
The following code (especially line 66) https://github.com/Smile-SA/magento2-module-custom-entity-product-link/blob/3adc1be0d40c8b0440b1edc2745eef719a0cc4e3/Observer/Catalog/Product/AddCustomEntitiesInformation.php#L65-L69
causes the issue when custom entity is not assigned to a product in case when the same custom entity is linked with different products (shown on the same page) by different product attributes, e.g.:
- Product SKU=001 linked to the custom entity by
custom_attr_001
- Product SKU=002 linked to the custom entity by
custom_attr_002
- Both products are shown on the same page (e.g. category page)
Actual Result:
- Product SKU=001 has no the custom entity assigned
- Product SKU=002 has the custom entity assigned by
custom_attr_002
Expected Results:
- Product SKU=001 has the custom entity assigned by
custom_attr_001
- Product SKU=002 has the custom entity assigned by
custom_attr_002
cloning workaround
foreach ($customEntities as $customEntity) {
- $customEntity->setProductAttributeCode($attributeCode);
- $attributeValues[$attributeCode][] = $customEntity->getId();
- $productCustomEntities[] = $customEntity;
+ $productCustomEntity = clone $customEntity;
+ $productCustomEntity->setProductAttributeCode($attributeCode);
+ $attributeValues[$attributeCode][] = $productCustomEntity->getId();
+ $productCustomEntities[] = $productCustomEntity;
}