WooCommerce Add Shipping Class to 1000 Products

I’ve just updated my WooCommerce Shipping Classes to work better with free shipping on certain items.

However, I need to apply my new ‘Standard Shipping’ class to 1000+ Products. Going through Products > Bulk Edit can’t handle selecting them all at once, nor 100 at once, I don’t want to resort to going through 10 by 10 until I know there’s no other way of doing this.

Read More

Question: Is there an SQL query that can speed this up?

I can’t seem to find where in the database the Shipping Class is kept for each product 🙁

Related posts

Leave a Reply

2 comments

    1. In screen option at top of the page choose to show 1000 products per
      page and remove all other fields except title.

    2. Select all products

    3. Bulk Edit

  1. I made this plugin, it loops through all the products of a certain category(s) and assigns the desired shipping class, more details are in the comments:

    <?php
    /*
    Plugin Name: Bulk Shipping class update
    Description: Bulk Update of shipping class for woocommerce products
    Version: 0.0.1
    Contributors: svelandiag
    Author: svelandiag
    Text Domain: woocommerce-ship-class-by-cat
    */
    
    
    // If this file is called directly, abort.
    if ( ! defined( 'WPINC' ) ) {
        die;
    }
    
    // define the woocommerce_init callback 
    function action_woocommerce_init( $array ) { 
        /** The category ID's with the posts you want to change */
        $category_ids = array(635, 1022, 289, 291); // array of ints or single int
        
        /** The shipping class to set for the products */
        $shipping_class_slug = "shipping-class-slug"; // found in "shipping classes" in woocommerce settings
        
        /** Run query to collect our data */
        $products = new WP_Query(array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'fields' => 'ids',
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => $category_ids,
                    'operator' => 'IN'
                )
            )
        ));
        
        /** Set our shipping class on each product */
        foreach ($products->posts as $pid) {
            wp_set_object_terms($pid, $shipping_class_slug, 'product_shipping_class');
        }
        
        /** reset the query */
        wp_reset_query();
    }; 
             
    // add the action 
    add_action( 'admin_init', 'action_woocommerce_init', 10, 1 );
    
    ?>
    

    You can also extract the hook and the callback and add it to your functions.php of your child theme, be careful this is a one-time use snippet since it runs every time admin backend initializes.

    The plugin has been tested with the latest Woocomerce to date 4.x and latest WordPress 5.x as well.