Welcome to Sydney Business Web Technical Solutions
Every day, we solve problems for eCommerce website owners. We had a think about how we might use this activity to help others, and came up with this idea: Every week, we'll take the trickiest problem and publish our solution.
Important! - Some of these solututions involve adding code to your website (WordPress and Woocommerce mostly), so please ALWAYS be careful. We are not in any way responsible, directly or indirectly for any impact or consequences our code or advice has on your website, nor are we liable for any damage arising from such use.
Always back up your website before changing or adding code and/or editing the database, This is critically important!!!
PROBLEM 1: Could not Edit Products
Our customer informed us that any attempt to edit a product resulted in a time-out. This should never happen and indicates a possible database malfunction. We already had a databse optimization plugin installed: Advanced Database Cleaner.
(Note: This is installed on >100,000 websites but is currently lagging behind WordPress Updates. We have tested with WordPress 6.3 and Woocommerce 8.02)
What we found was a hundreds of Woocommerce exppired transients (transients in WooCommerce are a mechanism for caching time-sensitive data to enhance performance. They're an integral part of the WooCommerce system, helping reduce the load on the database and speeding up operations). But they do need to be cleaned up and this was not happening. So we decided to save some time and write some code to clear them safely, periodically.
Using the Code Snippets Plugin
Adding code to core files in WordPress and Woocommerce is simply inadvisable for a range of reasons. We always use a plugin called Code Snippets. It's simple, reliable, stable, and a pleasure to use. Next we wrote the php code which is copied below:
// Add this code to your theme's functions.php file or in a custom plugin
// Step 1: Function to clear WooCommerce transients
function clear_woocommerce_transients() {
if ( function_exists('wc_delete_expired_transients') ) {
wc_delete_expired_transients(); // Deletes expired transients
}
// Optionally, if you want to delete all WooCommerce transients (not just the expired ones):
// global $wpdb;
// $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_wc_%')" );
// $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_timeout_wc_%')" );
}
// Step 2: Set up WP-Cron job to run the above function every 2 hours
function setup_cron_to_clear_transients() {
if ( ! wp_next_scheduled( 'clear_woocommerce_transients_hook' ) ) {
wp_schedule_event(time(), '2hours', 'clear_woocommerce_transients_hook');
}
}
add_action('wp', 'setup_cron_to_clear_transients');
add_action('clear_woocommerce_transients_hook', 'clear_woocommerce_transients');
// Ensure '2hours' recurrence schedule exists
function add_2hours_cron_recurrence($schedules) {
$schedules['2hours'] = array(
'interval' => 2 * 60 * 60,
'display' => __('Every 2 Hours'),
);
return $schedules;
}
add_filter('cron_schedules', 'add_2hours_cron_recurrence');
//Copyright Sydney Business Web
Results
The plugin cleared expired Woocommerce transients and restored operation. We are still checlking to make sure it works every two hours.
Need help? -