WordPress Multitenant SaaS: How To Add A WooCommerce Product ID To WP-CONFIG

When you’re building a SaaS on WordPress that is not based on WordPress multisite, you typically want to push data into the wp-config.php file of each site.

You do this because you would want to access the data in a custom plugin on the tenant site. This plugin allows you to control the user experience and features available in the tenant site.

With WPCloudDeploy, you have hooks that you can use to add data to the wp-config.php site after it’s deployed.

For this scenario, the hook you want to work with is: wpcd_wpapp_install_complete_wc_before_emails.

Typically, in this hook, your code sequence would be as follows:

  • Get the product id from the WPCD site record and bail out if one does not exist.
  • Get a list of categories or product ids that you want to work with – maybe you don’t want this functionality for all products or categories or maybe you want to push different information for different products or categories
  • Call the WPCD action hook to push data in wp-config.

Here’s a typical sequence assuming you’re using a PHP Class:

require_once ABSPATH . 'wp-admin/includes/plugin.php';

class WPCDCLOUD_CustomFunctions_Init {

	/**
	 * Constructor function.
	 */
	public function __construct() {

		$plugin_data = get_plugin_data( __FILE__ );

		/* Do custom things after site is ready . */
		add_action( 'wpcd_wpapp_install_complete_wc_before_emails', array( $this, 'wp_install_complete' ), 20, 5 );

	}

	/**
	 * Perform after-site install actions by firing appropriate hooks on tenants.
	 *  - Update wp-config.php
	 *
	 * Action Hook: wpcd_wpapp_install_complete_wc_before_emails
	 *
	 * @see function wpcd_wpapp_install_complete in class-wordpress-wc-sell-site-subs.php.
	 *
	 * @param int    $id                 post id of server.
	 * @param int    $app_id             post id of wp app.
	 * @param string $name               command name executed for new site.
	 * @param string $base_command       basename of command.
	 * @param string $pending_task_type  Task type to update when we're done. This is not part of the action hook definition - it's only passed in explicitly when this is called as a function.
	 */
	public function wp_install_complete( $id, $app_id, $name, $base_command, $pending_task_type ) {

		// Lets get the product id from the app record.
		$product_id = (string) get_post_meta( $app_id, 'wpapp_wc_product_id', true );

		// Bail if we don't have a product id.
		if ( empty( $product_id ) ) {
			return;
		}

		// List of product categories that are site products.
		$site_categories = array( 1111, 2222 )

		// If the product id belongs to one of the categories we are working with, do somethings.
		if ( has_term( $site_categories, 'product_cat', $product_id ) ) {

			// Add woo-commerce product ids and category.
			do_action( 'wpcd_wordpress-app_do_update_wpconfig_option', $app_id, 'WC_PRODUCT_ID', $product_id, 'no' );
			do_action( 'wpcd_wordpress-app_do_update_wpconfig_option', $app_id, 'WC_CATEGORIES', implode( ',', $site_categories ), 'no' );

		}
		
		// Add some special things for other products.
		if ( in_array( $product_id, array( 1, 2, 3 ) ) ) {
			// add some other things to wp-config.php here using the same do_action hooks you see above.
		}

	}

}

In the code above, you’ll see we declare an array of WooCommerce category ids. If the product the user purchased is in one of these categories then the WooCommerce product id and the category list is added to the site’s wp-config.php file.

We also added an example section where you can do some things specific to product ids.

Wrap Up

You can use a custom plugin in your tenant site to examine the contents of the WC_PRODUCT_ID and or WC_CATEGORIES constant and take action on it. For example, you might limit the list of plugins that the user can see.

Was This Article Useful? Or do you have questions or comments about it (or our products & services)? We'd love to hear from you!

Please enter your name.
Please enter a message.
You must accept the Terms and Conditions.
Please check the captcha to verify you are not a robot.

Automatic Notification Of New Articles

Sign up to get automatic notifications of new articles.  This is a different list than our standard list - you only get new articles once a week (usually on Mondays).  No other emails will be sent unless you sign up for our general list as well.

Posted in