Skip to content

Commit 78727ca

Browse files
authored
Merge pull request #445 from rtCamp/try/woocommerce-purge-option
Feat: Add Purge Option For WooCommerce Plugin[develop]
2 parents b50815f + ba9d2e0 commit 78727ca

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ To purge a page immediately, follow these instructions:
113113

114114
Yes. When setting the URL structure in Nginx configuration file a trailing slash should always be added.
115115

116+
**Q. How can I automatically purge cache when WooCommerce product stock reduces after purchase?**
117+
118+
Nginx Helper now includes optional WooCommerce integration.
119+
If enabled, cache for purchased products will be purged automatically whenever stock is reduced after an order.
120+
121+
To use it:
122+
123+
1. Go to Nginx Helper → Settings → WooCommerce Options.
124+
2. Tick “Purge product cache on stock change or purchase”.
125+
126+
This ensures customers always see up-to-date stock status without stale cache.
127+
128+
Note: This option will only be visible if WooCommerce is active.
129+
116130
### FAQ - Nginx Redis Cache ###
117131

118132
**Q. Can I override the redis hostname, port and prefix?**

admin/class-nginx-helper-admin.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

admin/partials/nginx-helper-general-options.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
'purge_amp_urls',
5050
'preload_cache',
5151
'roles_with_purge_cap',
52+
'purge_woo_products'
5253
);
5354

5455
$all_inputs = array();
@@ -1050,6 +1051,25 @@
10501051
</table>
10511052
</div> <!-- End of .inside -->
10521053
</div>
1054+
<?php if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) : ?>
1055+
<div class="postbox enable_purge"<?php echo empty( $nginx_helper_settings['enable_purge'] ) ? ' style="display: none;"' : ''; ?>>
1056+
<h3 class="hndle">
1057+
<span><?php esc_html_e( 'WooCommerce Options', 'nginx-helper' ); ?></span>
1058+
</h3>
1059+
<div class="inside">
1060+
<table class="form-table">
1061+
<tr valign="top">
1062+
<td>
1063+
<input type="checkbox" value="1" id="purge_woo_products" name="purge_woo_products" <?php checked( $nginx_helper_settings['purge_woo_products'] ?? 0, 1 ); ?> />
1064+
<label for="purge_woo_products">
1065+
<?php esc_html_e( 'Purge product cache on stock change or purchase', 'nginx-helper' ); ?>
1066+
</label>
1067+
</td>
1068+
</tr>
1069+
</table>
1070+
</div> <!-- End of .inside -->
1071+
</div>
1072+
<?php endif; ?>
10531073
<input type="hidden" name="smart_http_expire_form_nonce" value="<?php echo esc_attr( wp_create_nonce( 'smart-http-expire-form-nonce' ) ); ?>" />
10541074
<?php
10551075
submit_button( __( 'Save All Changes', 'nginx-helper' ), 'primary large', 'smart_http_expire_save', true );

includes/class-nginx-helper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ private function define_admin_hooks() {
249249
$this->loader->add_action( 'upgrader_process_complete', $nginx_helper_admin, 'nginx_helper_auto_purge_on_any_update', 10, 2 );
250250
$this->loader->add_action( 'admin_notices', $nginx_helper_admin, 'suggest_purge_after_update' );
251251
$this->loader->add_action( 'admin_init', $nginx_helper_admin, 'dismiss_suggest_purge_after_update' );
252+
253+
// WooCommerce integration.
254+
$this->loader->add_action( 'plugins_loaded', $nginx_helper_admin, 'init_woocommerce_hooks' );
255+
252256
}
253257

254258
/**

readme.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ To purge a page immediately, follow these instructions:
107107

108108
Yes. When setting the URL structure in Nginx configuration file a trailing slash should always be added.
109109

110+
**Q. How can I automatically purge cache when WooCommerce product stock reduces after purchase?**
111+
112+
Nginx Helper now includes optional WooCommerce integration.
113+
If enabled, cache for purchased products will be purged automatically whenever stock is reduced after an order.
114+
115+
To use it:
116+
117+
1. Go to Nginx Helper → Settings → WooCommerce Options.
118+
2. Tick “Purge product cache on stock change or purchase”.
119+
120+
This ensures customers always see up-to-date stock status without stale cache.
121+
122+
Note: This option will only be visible if WooCommerce is active.
123+
110124
= FAQ - Nginx Redis Cache =
111125

112126
**Q. Can I override the redis hostname, port and prefix?**

0 commit comments

Comments
 (0)