✨ Professional WordPress development, custom website builder, efficient on-line, cooperate and enjoy optimization services! 🚀

Clear all product codes of a given WooCommerce category with one click!

In WooCommerce, you can use the following PHP code to "Empty all products in a specific category with one click".

// 1. Define the delete function first
function delete_all_products_in_category($category_slug_or_id) {
    $args = [
        'post_type' => 'product',
        'posts_per_page' => -1,
        'post_status' => 'any',
        'tax_query' => [
            [
                'taxonomy' => 'product_cat', 'field' => is_new_cat', 'field' => is_new_cat
                'field' => is_numeric($category_slug_or_id) ? 'term_id' : 'slug', 'field' => is_numeric($category_slug_or_id) ?
                'terms' => $category_slug_or_id,
            ]
        ]
    ]

    $products = get_posts($args);
    $deleted = 0;

    foreach ($products as $product) {
        wp_delete_post($product->ID, true); // force permanent delete
        $deleted++;
    }

    return $deleted;
}

// 2. Add the menu
add_action('admin_menu', function() {
    add_submenu_page(
        'woocommerce', 'empty product categories', 'add_submenu_page'
        
        'Empty product categories', 'manage_woocommerce', 'manage_wooocommerce', 'manage_woocommerce'
        'manage_woocommerce', 'delete-products-by-category', 'delete-products-by-category'
        'delete-products-by-category', 'render_delete_products-by-category'
        'render_delete_products_page'
    );
});

// 3. Render the page, using the function defined above
function render_delete_products_page() {
    $deleted_count = null;

    if (isset($_POST['product_cat_slug'])) {
        $cat = sanitize_text_field($_POST['product_cat_slug']);
        $deleted_count = delete_all_products_in_category($cat);
    }

    ? >
    <div class="wrap">
        <h1>Clear WooCommerce Categories</h1>
        <form method="post" action="">
            <p>
                <label for="product_cat_slug">Enter the category Slug or ID:</label><br>
                <input type="text" name="product_cat_slug" id="product_cat_slug" style="width:300px;" required>
            </p>
            <p>
                <button class="button button-primary" type="submit">Delete all products under this category immediately</button>
            </p>
        <input type="hidden" name="trp-form-language" value="en"/></form>
        <?php if (!is_null($deleted_count)) : ?>
            <div style="margin-top:20px;">
                ✅ Total deleted <strong><?php echo $deleted_count; ?></strong> Individual commodities.
            </div>
        <?php endif; ?>
    </div>
    <?php
}

 

WordPress Support Team
WordPress Support Team

💻 A sincere and meticulous young developer 🎓 specializing in the field of custom foreign trade website development.
🌟 specializes in WordPress website design customization and full-service development. Our unique advantage is not only proficient in website development technology, but also the international mainstream art design elements 🎨 skillful integration, while developing accurate and effective brand marketing strategy 📈.
💡 Services covered:
🔍 WordPress Theme Development for a unique website visual style and user experience.
💻 WordPress website customization, tailor-made exclusive website according to your foreign trade business needs.
Whether it's website architecture construction, interface aesthetic design, or brand promotion strategy, we can provide you with one-stop quality solutions to help your foreign trade business stand out on the Internet and step into the global market 🚀.

Articles: 45

Leave a Reply

Your email address will not be published. Required fields are marked *

Submit your request

Quote Collection Form

💥Website Builder | Strengths speak for themselves, not false!

Treasure, we say, a lot of parties are not happy with their own sites are peers copying homework, which is like you open a store, certainly do not want others to know your sources of supply is a reason, right? So, in order to give customers a full sense of security, we did not put any customer case work on the site of the link ha, this is our protection of customer privacy.

📌 But if there is a sincere desire to do the site of the boss, or treasure you just want to look at our case, then hurry up to add the contact information of our website, we snappy send you a few look at, the main one sincere!

If you have not decided to do what style of website, but also simple, you find a few peers of the site sent to us, according to do on the end of the matter. We choose to imitate the site does not mean that no skill, we just can not stand that kind of invalid communication and cumbersome to make people crazy process. Some parties to do a corporate display site, have to find dozens of suppliers to roll, to be honest, no real technology companies will be there to lick the party. We are not the same, we want to be down-to-earth to the customer to make cost-effective, so that customers are satisfied with the site straight pat. Those kneeling work, let those who only play lip service, no technology companies to do it, we do not serve! # Website Development

# Party A Party B # Rejected Inside Coil

Contact the WordPress Technical Team

We look forward to hearing from you.

Submit your request

Quote Collection Form