Smile-SA / magento2-module-custom-entity-product-link

31 stars 12 forks source link

Custom entity lost in case it's assigned to multiple products on the same page by different attributes #45

Open yutv opened 2 years ago

yutv commented 2 years ago

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:

Expected Results:

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;
     }