magento2-module-custom-entity-product-link icon indicating copy to clipboard operation
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

Open yutv opened this issue 2 years ago • 0 comments

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.:

  1. Product SKU=001 linked to the custom entity by custom_attr_001
  2. Product SKU=002 linked to the custom entity by custom_attr_002
  3. 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;
     }

yutv avatar Nov 25 '22 20:11 yutv