Skip to content

Commit 68a517c

Browse files
authored
Merge pull request #452 from rtCamp/try/woo-purging-rest-api-master
Feat: Extend WooCommerce Purging Functionality For REST API [master]
2 parents 52c9e6e + 35ee920 commit 68a517c

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ Yes. When setting the URL structure in Nginx configuration file a trailing slash
116116
**Q. How can I automatically purge cache when WooCommerce product stock reduces after purchase?**
117117

118118
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.
119+
If enabled, cache for purchased products or products updated via REST API will be purged automatically.
120120

121121
To use it:
122122

123123
1. Go to Nginx Helper → Settings → WooCommerce Options.
124-
2. Tick “Purge product cache on stock change or purchase”.
124+
2. Tick “Purge product cache on updates”.
125125

126-
This ensures customers always see up-to-date stock status without stale cache.
126+
This ensures customers always see up-to-date stock status and product information without stale cache.
127127

128128
Note: This option will only be visible if WooCommerce is active.
129129

admin/class-nginx-helper-admin.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@ public function init_woocommerce_hooks() {
10611061
}
10621062

10631063
add_action( 'woocommerce_reduce_order_stock', array( $this, 'purge_product_cache_on_purchase' ), 10, 1 );
1064+
add_action( 'woocommerce_update_product', array( $this, 'purge_product_cache_on_update' ), 10, 1 );
10641065
}
10651066

10661067
/**
@@ -1100,4 +1101,32 @@ public function purge_product_cache_on_purchase( $order ) {
11001101
}
11011102
}
11021103
}
1104+
1105+
/**
1106+
* Purge product cache when a product is updated via REST API.
1107+
*
1108+
* @since 2.3.5
1109+
* @global object $nginx_purger Nginx purger object.
1110+
* @param int $product_id Product ID.
1111+
*/
1112+
public function purge_product_cache_on_update( $product_id ) {
1113+
global $nginx_purger;
1114+
1115+
if ( empty( $nginx_purger ) ) {
1116+
return;
1117+
}
1118+
1119+
if ( ! $this->options['enable_purge'] ) {
1120+
return;
1121+
}
1122+
1123+
$nginx_purger->log( 'WooCommerce product update - purging cache for product ID: ' . $product_id );
1124+
1125+
$product_url = get_permalink( $product_id );
1126+
1127+
if ( $product_url ) {
1128+
$nginx_purger->purge_url( $product_url );
1129+
}
1130+
}
1131+
11031132
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@
10621062
<td>
10631063
<input type="checkbox" value="1" id="purge_woo_products" name="purge_woo_products" <?php checked( $nginx_helper_settings['purge_woo_products'] ?? 0, 1 ); ?> />
10641064
<label for="purge_woo_products">
1065-
<?php esc_html_e( 'Purge product cache on stock change or purchase', 'nginx-helper' ); ?>
1065+
<?php esc_html_e( 'Purge product cache on updates', 'nginx-helper' ); ?>
10661066
</label>
10671067
</td>
10681068
</tr>

readme.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ Yes. When setting the URL structure in Nginx configuration file a trailing slash
110110
**Q. How can I automatically purge cache when WooCommerce product stock reduces after purchase?**
111111

112112
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.
113+
If enabled, cache for purchased products or products updated via REST API will be purged automatically.
114114

115115
To use it:
116116

117117
1. Go to Nginx Helper → Settings → WooCommerce Options.
118-
2. Tick “Purge product cache on stock change or purchase”.
118+
2. Tick “Purge product cache on updates”.
119119

120-
This ensures customers always see up-to-date stock status without stale cache.
120+
This ensures customers always see up-to-date stock status and product information without stale cache.
121121

122122
Note: This option will only be visible if WooCommerce is active.
123123

0 commit comments

Comments
 (0)