Skip to content

Instantly share code, notes, and snippets.

@johnlewisdesign
Last active June 18, 2020 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnlewisdesign/8d5d06e83e3dcc45807dc60b4979ca56 to your computer and use it in GitHub Desktop.
Save johnlewisdesign/8d5d06e83e3dcc45807dc60b4979ca56 to your computer and use it in GitHub Desktop.
Change shipping class en masse [WordPress/WooCommerce]
<?php
/** The category IDs containing the posts you want to change */
$category_ids = array(48, 50, 51, 52, 53); // array of ints or single int
/** The shipping class to set for the products */
$shipping_class_slug = "postcards"; // 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment