WooCommerce: assign a category to all products

Anatole

I have imported tens of thousands of products into Woocommerce but they are not assigned to any category. How can I assign a category (for example the default category) to all the products in my catalog? Thanks

The option to do it with the back office would be much too long of course, I'm looking for the SQL query instead, or a plugin maybe?

Snuffy

Something like that should do the trick. Backup before doing it !

UPDATE wp_term_relationships AS tr
JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms AS t ON tt.term_id = t.term_id
JOIN wp_posts AS p ON tr.object_id = p.ID
SET tt.term_taxonomy_id = {your_category_id}
WHERE tt.taxonomy = 'product_cat'
  AND p.post_type = 'product';

Replace wp_ with your prefix if its different. Replace {your_category_id} with the ID of the category you aim.

Another way is using a function

function assign_all_products_to_category() {
    $category_id = 123; // Replace with the actual ID of your target product category

    // Get all product IDs
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => -1,
    );
    $products = get_posts( $args );

    if ( $products ) {
        foreach ( $products as $product ) {
            $product_id = $product->ID;
            wp_set_object_terms( $product_id, $category_id, 'product_cat', true );
        }
        echo 'All products have been assigned to the category successfully.';
    } else {
        echo 'No products found.';
    }
}

Execute the function as you see fit.

For example run it as url

function check_custom_param() {
    if ( isset( $_GET['run_function'] ) && $_GET['run_function'] === 'assign_products' ) {
        assign_all_products_to_category();
        exit; // Terminate script execution after running the function
    }
}
add_action( 'template_redirect', 'check_custom_param' );

Your url should look like: http://example.com/?run_function=assign_products

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Display All Products by Category with WooCommerce

Automatically assign products to a defined product category in WooCommerce

WooCommerce hide all products of a certain category if not loggedin

Get all WooCommerce products from a category that have specific meta data

How to get all products from current WooCommerce product category?

how to show a category of woocommerce products

woocommerce shortcode for products in brand and category

shortcode to show products by category woocommerce

Woocommerce products from category loop

How to assign all products in a category to all it's ancestor categories programmatically, in Magento 2

Get current product category all products parent sku's and display them in array - Woocommerce

Get all Woocommerce products from current product category term Id in a WP_Query

How to sort products in category by post id in WooCommerce?

Woocommerce MySQL to update price of products in specific category

Exclude a product category from Woocommerce related products

Exclude a product category from Related products in WooCommerce

Fetch products by category in Woocommerce REST API

Woocommerce sort products by category using hook or filter

Hide products having a specific product category in Woocommerce

Check if a Category contains any products that are on sale in WooCommerce?

Adding "Sale" product category to products that are on sale in Woocommerce

Display products from specific product category in Woocommerce

Nested shortcode to dynamically display WooCommerce products category

Woocommerce, get products by category with wp query

Display with a shortcode a dropdown of Woocommerce products for a specific category

WooCommerce category page showing products and subcategories

Exclude Products Category from loops shortcode Woocommerce

Apply discount on products within specific category (WooCommerce)

Exclude Woocommerce products in recently viewed products widget from a product category