Tax update

<?php

// tuinenshop
// Bulk update all prices with the current tax method: Navigate to any admin page and add update_prices (only once!) to the url including page number.
add_action('admin_head', function () {
	if (isset($_GET['update_prices'])) {
		$args = array(
		'post_type' => 'product',
		'posts_per_page' => 1000,
		'paged' => $_GET['update_prices']
		);
		$loop = new WP_Query($args);
		$prices = ['_price', '_sale_price', '_regular_price'];
		if ($loop->have_posts()) {
			while ($loop->have_posts()) :
				$loop->the_post();
				$product = wc_get_product(get_the_ID());
				$tax_rates = WC_Tax::get_rates($product->get_tax_class());
				if (!empty($tax_rates)) {
					$rate = reset($tax_rates)['rate'];
					if ($rate) {
						foreach ($prices as $price_meta) {
							$oldprice = get_post_meta(get_the_ID(), $price_meta, true);
							if ($oldprice) {
								$newprice = $oldprice * ($rate / 100 + 1);
								update_post_meta(get_the_ID(), $price_meta, $newprice);
							}
						}
					}
				}
			endwhile;
		}
		wp_reset_postdata();
	}
});
Last Updated:
Contributors: Niek Vlam, Suite Seven