Last active
June 18, 2020 19:36
-
-
Save johnlewisdesign/8d5d06e83e3dcc45807dc60b4979ca56 to your computer and use it in GitHub Desktop.
Change shipping class en masse [WordPress/WooCommerce]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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