从Woocommerce中删除重复的项目

时间:2018-04-26 15:48:23

标签: php wordpress woocommerce

我在导入产品时遇到问题。现在有些产品是重复的。所有重复的产品都具有相同的标题和属性(collection_id)。还有很多重复的媒体文件。有没有办法删除重复的产品?至少我想删除产品。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我确实遇到了。

1.- 在我的数据库中发现重复的产品(按 sku 搜索)与查询:

SELECT meta_value, count(*) AS total FROM wp_postmeta WHERE meta_key = '_sku' GROUP BY meta_value HAVING total > 1

2.- 我用这些 skus 创建了一个名为 wp_repeated 的新表:

CREATE TABLE wp_repeated AS SELECT meta_value, count(*) AS total FROM wp_postmeta WHERE meta_key = '_sku' GROUP BY meta_value HAVING total > 1

3.- 我从这些 skus 那里得到了 post_id:

SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)

4.- 最后,从所有出现 post_id 的表中移除和删除:

DELETE relations.*, taxes.*, terms.* FROM wp_term_relationships AS relations INNER JOIN wp_term_taxonomy AS taxes ON relations.term_taxonomy_id=taxes.term_taxonomy_id INNER JOIN wp_terms AS terms ON taxes.term_id=terms.term_id WHERE object_id IN (SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)); DELETE FROM wp_postmeta WHERE post_id IN ( SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)); DELETE FROM wp_posts WHERE ID IN (SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated));

希望对你有帮助。问候。

TAKE CARE:这些查询会删除所有重复的产品。