Last active
January 9, 2024 18:31
-
-
Save Tabrisrp/582224da56be2f765302527edfaf95f7 to your computer and use it in GitHub Desktop.
Clear WP Rocket cache after an order is made
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 | |
/** | |
* Plugin Name: WP Rocket clean post cache after WC order | |
* Description: Clean the cache for each product ordered after a completed WooCommerce order | |
* Author: WP Rocket team | |
* License: GNU General Public License v3 or later | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
// Basic security, prevents file from being loaded directly. | |
defined( 'ABSPATH' ) or die( 'Cheatin’ uh?' ); | |
add_action('woocommerce_payment_complete', 'rocket_clean_cache_after_order', 10, 1); | |
function rocket_clean_cache_after_order( $order_id ) { | |
$order = new WC_Order( $order_id ); | |
$items = $order->get_items(); | |
foreach ( $items as $item ) { | |
rocket_clean_post( $item['product_id'] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
after placing an order, wp rocket clears the cache of other websites, which I don't want. After placing the order, only the product page should be cleared to update the stock status.
log from 02-partial-cache-clear.txt
`*****************************************************************
[2024-01-09 18:02:05]
#CALLSTACK#####
POST
ID: 17490
URL: https://swiat-zosi.pl/sklep/domek-dla-lalek-drewniany-mebelk-pastel-114cm/
Post_type: product
#PURGED_URLS#####
Array
(
[0] => https://swiat-zosi.pl/sklep/domek-dla-lalek-drewniany-mebelk-pastel-114cm/
[1] => https://swiat-zosi.pl/sklep/
[4] => https://swiat-zosi.pl/blog/
[6] => https://swiat-zosi.pl/author/lew-shop/
)`