diff --git a/app/code/Magento/Catalog/Plugin/Model/Product/Relation/RemoveRelations.php b/app/code/Magento/Catalog/Plugin/Model/Product/Relation/RemoveRelations.php new file mode 100644 index 0000000000000..765d925468f4b --- /dev/null +++ b/app/code/Magento/Catalog/Plugin/Model/Product/Relation/RemoveRelations.php @@ -0,0 +1,86 @@ +metadataPool = $metadataPool; + $this->resourceConnection = $resourceConnection; + $this->logger = $logger; + } + + /** + * Delete related product relations + * + * @param ProductResource $subject + * @param ProductResource $result + * @param ProductInterface $product + * @return ProductResource + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @throws \Exception + */ + public function afterDelete( + ProductResource $subject, + ProductResource $result, + ProductInterface $product + ): ProductResource { + $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $connection = $this->resourceConnection->getConnection(); + try { + $connection->beginTransaction(); + $relationTable = $this->resourceConnection->getTableName('catalog_product_relation'); + $whereCondition = $connection->quoteInto('child_id = ?', $product->getId()) + . " OR " + . $connection->quoteInto('parent_id = ?', $product->getData($linkField)); + $connection->delete($relationTable, $whereCondition); + $connection->commit(); + } catch (\Exception $e) { + $connection->rollBack(); + $this->logger->error(\sprintf( + 'Could not delete product relations for product %1$s. %2$s', + $product->getId(), + $e + )); + } + return $result; + } +} diff --git a/app/code/Magento/Catalog/etc/db_schema.xml b/app/code/Magento/Catalog/etc/db_schema.xml index ddd66a5bf04bd..5926e10483e65 100644 --- a/app/code/Magento/Catalog/etc/db_schema.xml +++ b/app/code/Magento/Catalog/etc/db_schema.xml @@ -1069,12 +1069,6 @@ - - diff --git a/app/code/Magento/Catalog/etc/db_schema_whitelist.json b/app/code/Magento/Catalog/etc/db_schema_whitelist.json index f4cda73c371d0..97db6962aa5ec 100644 --- a/app/code/Magento/Catalog/etc/db_schema_whitelist.json +++ b/app/code/Magento/Catalog/etc/db_schema_whitelist.json @@ -644,9 +644,7 @@ "CATALOG_PRODUCT_RELATION_CHILD_ID": true }, "constraint": { - "PRIMARY": true, - "CAT_PRD_RELATION_CHILD_ID_CAT_PRD_ENTT_ENTT_ID": true, - "CAT_PRD_RELATION_PARENT_ID_CAT_PRD_ENTT_ENTT_ID": true + "PRIMARY": true } }, "catalog_product_index_eav": { diff --git a/app/code/Magento/Catalog/etc/di.xml b/app/code/Magento/Catalog/etc/di.xml index 8a116282e2578..a947b9af7542e 100644 --- a/app/code/Magento/Catalog/etc/di.xml +++ b/app/code/Magento/Catalog/etc/di.xml @@ -253,6 +253,9 @@ Magento\Catalog\Model\Session\Storage + + +