@@ -294,7 +294,8 @@ public function nginx_helper_default_settings() {
294294 'redis_acl_enabled_by_constant ' => 0 ,
295295 'preload_cache ' => 0 ,
296296 'is_cache_preloaded ' => 0 ,
297- 'roles_with_purge_cap ' => array ()
297+ 'roles_with_purge_cap ' => array (),
298+ 'purge_woo_products ' => 0 ,
298299 );
299300
300301 }
@@ -1048,4 +1049,55 @@ public function dismiss_suggest_purge_after_update() {
10481049 exit ;
10491050 }
10501051 }
1052+
1053+ /**
1054+ * Initialize WooCommerce hooks if enabled.
1055+ *
1056+ * @since 2.3.5
1057+ */
1058+ public function init_woocommerce_hooks () {
1059+ if ( ! is_plugin_active ( 'woocommerce/woocommerce.php ' ) || empty ( $ this ->options ['purge_woo_products ' ] ) ) {
1060+ return ;
1061+ }
1062+
1063+ add_action ( 'woocommerce_reduce_order_stock ' , array ( $ this , 'purge_product_cache_on_purchase ' ), 10 , 1 );
1064+ }
1065+
1066+ /**
1067+ * Purge product cache when order stock is reduced (purchase).
1068+ *
1069+ * @since 2.3.5
1070+ * @global object $nginx_purger Nginx purger object.
1071+ * @param object $order Order object.
1072+ */
1073+ public function purge_product_cache_on_purchase ( $ order ) {
1074+
1075+ global $ nginx_purger ;
1076+
1077+ if ( ! $ order instanceof WC_Order ) {
1078+ return ;
1079+ }
1080+
1081+ if ( ! $ this ->options ['enable_purge ' ] ) {
1082+ return ;
1083+ }
1084+
1085+ $ nginx_purger ->log ( 'WooCommerce order stock reduction - purging product caches ' );
1086+
1087+ foreach ( $ order ->get_items () as $ item ) {
1088+ $ product = $ item ->get_product ();
1089+ if ( ! $ product ) {
1090+ continue ;
1091+ }
1092+
1093+ $ product_id = $ product ->get_id ();
1094+ $ nginx_purger ->log ( 'Purging cache for product ID: ' . $ product_id . ' due to purchase ' );
1095+
1096+ $ product_url = get_permalink ( $ product_id );
1097+
1098+ if ( $ product_url ) {
1099+ $ nginx_purger ->purge_url ( $ product_url );
1100+ }
1101+ }
1102+ }
10511103}
0 commit comments