Hi,
There is no direct way to do this in the plugin.
You can use create a must use plugin which would look something like this
function 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’] );
clean_post( $post_id );
}
}
add_action( ‘woocommerce_payment_complete’, ‘clean_product_cache_after_order’ );
Thread Starter
ibro87
(@ibro87)
Hi there,
I know there is no direct way to do this in the plugin so I was expecting to see custom function like this. Unfortunately, this function caused critical error on my website. Is there anything else we can try to make this work? Thanks again for your help!
Hi,
The code was really intended as a guide, try this instead (you will want to add a namespace at the top)
<?php
defined( 'ABSPATH' ) or die();
function 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'] );
purge_cache ( $post_id );
}
}
add_action( 'woocommerce_payment_complete', __NAMESPACE__ . '\clean_product_cache_after_order' );
Thread Starter
ibro87
(@ibro87)
Hey vupdraft,
Thank you so much for sending the modified code. Although there is no critical error this time, this code makes WooCommerce products not-purchasable so I can’t use it. 🙁
Could you please add this option in the plugin settings instead or provide me with the complete (working) code that I can just copy and paste in my function.php file or MU plugin?
I know some other plugins like WP Fastest Cache purge cache automatically when WooCommerce order status is changed, but I’d like to keep using WP-Optimize because of user specific cache feature which they do not have so thank you for your understanding and continual support!
HI,
I will add a feature request to incorporate this as one of our options
Thread Starter
ibro87
(@ibro87)
Ok, that sounds good.
In the meantime, would you be able to provide me custom (working) code that would allow me to use missing functionality ? Perhaps, this can help?
Generally we don’t do customisation requests via forums.
I did find some hooks that might be useful for you.
Try adding something like the following to your theme functions.php
add_action( ‘woocommerce_product_set_stock’, ‘wpo_flush_wc_purchase’, 999 );
add_action( ‘woocommerce_variation_set_stock’, ‘wpo_flush_wc_purchase’, 999 );
function wp_flush_wc_purchase() {
wpo_cache_flush();
}
Thread Starter
ibro87
(@ibro87)
Hi vupdraft,
Thanks again for the modified code and trying to complement the missing feature. Unfortunately, this code caused another critical error on the website and can’t be used. I do not really understand why it is so difficult to get this function to work with WP-Optimize plugin.
I guess there is nothing else we can do here so I will temporarily switch to “WP Fastest Cache” plugin until this functionality becomes available in WP-Optimize settings.
I will also make sure to mention your willingness to help in the review section. This thread can be closed now.
Thread Starter
ibro87
(@ibro87)
UPDATE: Thanks to the hooks provided, I managed to figure out this myself. For anyone who might need to clear the cache automatically after WooCommerce order is made, the code below will do it.
add_action(‘woocommerce_payment_complete’, ‘wpo_flush_wc_purchase’, 10, 1);
function wpo_flush_wc_purchase ( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
wpo_cache_flush ( $item[‘product_id’] );
}
}