如何快速刪除woocommerce 相同名稱的商品

假如您仍希望避免使用付費的產品匯入外掛,你可能會在商品欄內發現有許多重複商品,然而這個問題野一直是許多朋友們詢問的問題。

很幸運的是,這個問題其實可以透過簡單的 php來清除您網站上重複的商品。

// Connect to the database
global $wpdb;

// Get a list of all products with duplicate names
$duplicates = $wpdb->get_results("
    SELECT post_title, COUNT(*) as count
    FROM {$wpdb->posts}
    WHERE post_type = 'product'
    GROUP BY post_title
    HAVING COUNT(*) > 1
");

// Loop through each set of duplicates and delete all but the first product
foreach ($duplicates as $duplicate) {
    $products = $wpdb->get_results("
        SELECT ID
        FROM {$wpdb->posts}
        WHERE post_type = 'product' AND post_title = '{$duplicate->post_title}'
        ORDER BY ID ASC
    ");

    // Delete all but the first product
    $first_product = true;
    foreach ($products as $product) {
        if ($first_product) {
            $first_product = false;
        } else {
            wp_delete_post($product->ID, true);
        }
    }
}

執行完畢(清除完畢後請記得關閉)

也可以使用外掛來處理喔(只是依樣要付費才能夠使用該功能)