0

We are trying to update the related products with the default CSV import of Magento but instead of specifying the Empty attribute value constant, it is not removing the existing related products and if we add new SKUs for related products in CSV file, it appends with the existing related products. We need need to remove all the related products or want to have some option to update the related products with the new SKUs and remove the old ones. Is there any method or class that I can use for this?

I'm using Magento 2.3.3 CE.

1 Answer 1

0

The fastest way you can delete the related products is by using Mysql queries. Please check below an example as I performed this operation on a product from the Magento sample data db (you just have to replace the SKU and product ID with your own data):

  1. Get the relation type ID, in your case it's 'relation' for related products relation by using the query:
mysql> select * from catalog_product_link_type where code='relation';
+--------------+----------+
| link_type_id | code     |
+--------------+----------+
|            1 | relation |
+--------------+----------+
1 row in set (0.00 sec)
  1. Find your product IDs based on the SKUs you know you have and need the deletion of the related products associated to them:
mysql> select entity_id from catalog_product_entity where sku in ('24-UG06');
+-----------+
| entity_id |
+-----------+
|        15 |
+-----------+
1 row in set (0.00 sec)
  1. Visualize the associated products:
mysql> select * from catalog_product_link where product_id in (15) and link_type_id=1;
+---------+------------+-------------------+--------------+
| link_id | product_id | linked_product_id | link_type_id |
+---------+------------+-------------------+--------------+
|    2281 |         15 |                16 |            1 |
|    2282 |         15 |                17 |            1 |
|    2283 |         15 |                32 |            1 |
|    2284 |         15 |                34 |            1 |
+---------+------------+-------------------+--------------+
4 rows in set (0.00 sec)
  1. Delete the related products:
mysql> delete from catalog_product_link where product_id in (15) and link_type_id=1;
Query OK, 4 rows affected (0.00 sec)
  1. Check the results, you should have no related products for your product:
mysql> select * from catalog_product_link where product_id in (15) and link_type_id=1;
Empty set (0.00 sec)

As you can notice, I used the 'IN' condition in my queries. I did so so that you can reuse the same queries and instead of one value, you can replace the SKU and ID between brakets with a list of SKUs and product IDs if you need to run this over multiple products.

Good Luck!

2
  • 1
    Thanks, Diana for the help. I've written a custom module with your query and it is working perfectly fine. Mar 19, 2021 at 10:45
  • Glad to hear this! you are most welcome!
    – Diana
    Mar 19, 2021 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.