Update products on stock after new Woocommerce orders

If you're using Woocommerce for your e-commerce site, and you notice that products on stock don't get correctly updated, you can use this (MU) plugin to clear WP Rocket cache every time a new order is completed.

Clear cache via custom MU Plugin:

Follow the general instructions in this article:
How to Make a Custom (MU) Plugin

1
Create a folder named mu-plugins in your wp-content folder.
In case a mu-plugins folder already exists, don’t create a new one, just use the existing one.
2
Using a basic text editor create a new file and put the following code in it:
<?php
defined( 'ABSPATH' ) or die( 'No direct access here, kiddo.' );
/**
 * Plugin Name: WP Rocket | Clean Post Cache after WooCommerce Order
 * Description: Cleans the cache for each product ordered after a WooCommerce order has been completed.
 * Plugin URI:  http://docs.wp-rocket.me/article/881-update-products-on-stock-after-new-woocommerce-orders
 * Author:      WP Rocket Support Team
 * Author URI:  http://wp-rocket.me/
 * License:     GNU General Public License v3 or later
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */


/**
 * Clean post cache after order has been completed.
 * 
 * @param  int $order_id Order ID
 * @return void
 */
function wp_rocket_wc_clean_product_cache_after_order( $order_id ) {

	$order  = new WC_Order( $order_id );
	$items  = $order->get_items();

	foreach ( $items as $item ) {
		$post_id = absint( $item['product_id'] );
		rocket_clean_post( $post_id );
	}
}
add_action( 'woocommerce_payment_complete', 'wp_rocket_wc_clean_product_cache_after_order', 10 );
	
3
Save the file as  wp-rocket-wc-clean-product-cache.php
4
Upload this file to the mu-plugins folder created in step 1.
5
Finally, make sure to deactivate and re-activate WP Rocket.