Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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