Skip to content

Greg Sullivan

Enabling staging mode for WooCommerce Subscriptions on a SiteGround staging server

To prevent duplicate payments, WooCommerce Subscriptions attempts to confirm it’s being run in the production environment prior to charging customers. The method it uses to detect non-production environments fails on SiteGround staging servers, leading Subscriptions to bill customers in staging environments as well.

When a client reached out saying they’d created a staging server and were now hearing from customers who’d been charged twice on the same day, I initially doubted Subscriptions as the cause of the issue since I’d seen it automatically switch to staging mode for other clients. While working to find the source of these duplicate payments, though, I examined the plugin’s approach to detecting non-production environments. As explained in its documentation, “Subscriptions stores a clone of WordPress’s siteurl value in an option with the name wc_subscriptions_siteurl. It compares this value with the siteurl value to see if it has changed.”

(Subscriptions also injects the string _[wc_subscriptions_siteurl]_ into the middle of wc_subscriptions_siteurl so that it isn’t modified by an automated process responsible for copying the production site to a staging environment—one that might replace all instances of the siteurl value with the staging URL.)

However, SiteGround doesn’t take a traditional approach to its siteurl on staging servers and instead uses the same value on staging and production. (It accomplishes this by modifying the value of HTTP_HOST in the staging environment to match that of the production environment.)

To overcome this issue, I first needed a way to detect SiteGround’s staging environment. With that taken care of, I filtered woocommerce_subscriptions_is_duplicate_site so it always returns true:

// Check whether SiteGround's staging environment has been detected.
if ( defined( 'SITEGROUND_STAGING' ) && SITEGROUND_STAGING ) {
	// Always flag the staging environment as a duplicate site.
	add_filter( 'woocommerce_subscriptions_is_duplicate_site', '__return_true' );
}Code language: PHP (php)

Added to a must-use plugin, the above snippet ensures that Subscriptions treats the SiteGround staging environment as a duplicate site, preventing customer charges on that instance of WordPress.