Allowing WooCommerce extensions to work with Classic Commerce

Classic Commerce Github issue 131 discusses the problem where some WooCommerce extensions won’t activate unless they detect that WooCommerce is installed and active.

A filter was proposed to fool the extensions into thinking WC was active, but unfortunately it doesn’t work.

However, an empty placeholder plugin structured as woocommerce/woocommerce.php does work.

I created one and it enabled me to use the Wirecard payment gateway extension, which wouldn’t work when it didn’t believe WC was present and active. I had to set the version to a high number to stop ClassicPress trying to find updates for it on the WP repo. I set it to version 9999.0.

I can’t attach zips here but I attached it to a comment on the Github issue.

5 Likes

It is possible that a little plugin could be written to help people overcome this problem. If I get time, I’ll have a look at it myself. But here is the hint, if someone else wants to tackle the issue.

I wrote a little plugin that puts a note on the admin bar to show any active orders that have been made, so I found that the plugin needs a test as follows to check if Woocommerce exists.

        /*
		 * Test to see if Woocommerce is active
		 */
		if ( is_plugin_active('woocommerce/woocommerce.php') ) {
            add_action( 'admin_bar_menu', 'ocws_wcab_newmenu',20 );
        } // end of testing for WooCommerce

I amended this as follows, so that it looks for either Woocommerce of Classic Commerce.

        /*
		 * Test to see if either Woocommerce or Classic Commerce are active
		 */
		if ( (is_plugin_active('woocommerce/woocommerce.php'))  || (is_plugin_active('classic-commerce/woocommerce.php')) ) {
            add_action( 'admin_bar_menu', 'ocws_wcab_newmenu',1000 );
        } // end of testing for WooCommerce or Classic Commerce

It should be possible to write a plugin that would force any other Woocommerce extension to accept Classic Commerce instead.

2 Likes

Surely the easiest way to accomplish this is just to add the necessary entry to the relevant field in the options table, like this:

function kts_options() {
	$active = get_option( 'active_plugins' );
	if ( ! in_array( 'woocommerce/woocommerce.php', $active ) ) {
		$active[] = 'woocommerce/woocommerce.php';
	}
	update_option( 'active_plugins', $active );
}
add_action( 'plugins_loaded', 'kts_options', 999 );

EDITED to add a check to see if it is already there.

1 Like

Of course, you’d also need to remove it when Classic Commerce is deactivated.

1 Like

This is a bit cleaner:


function kts_options() {
	$active = get_option( 'active_plugins' );
	if ( in_array( 'woocommerce/woocommerce.php', $active ) ) {
		return;
	}
	$active[] = 'woocommerce/woocommerce.php';
	update_option( 'active_plugins', $active );
}
add_action( 'plugins_loaded', 'kts_options', 999 );
2 Likes

Missing a closing ) on the if check statement
if ( ! in_array( 'woocommerce/woocommerce.php', $active ) )

1 Like

Yes, now added on the updated code. Thanks!

2 Likes

This would still require the user to have a dummy woocommerce in place for it to work. A compatibility plugin was discussed on Slack which led to this Gist that can be turned into a plugin named woocommerce/woocomerce.php

1 Like

What do you mean about a “dummy woocommerce”? All this involves is adding it as a function within Classic Commerce, and then another function to remove it if CC is deactivated.

1 Like

woocommerce/woocommerce.php Needs to exist in the plugin directory as CP will be looking for it when that function runs.

If the original WooCommerce is found then it will throw a fatal error so a dummy WooCommerce (A compatibility plugin so to speak) would need to be present in order for CP not to throw The plugin woocommerce/woocommerce.php has been deactivated due to an error: Plugin file does not exist. notice.

2 Likes

This is correct. A hook solution doesn’t work and this was noted in the Github issue linked above. All the file needs to contain is this:

<?php
/*
Plugin Name: WooCommerce Placeholder
Description: Makes WooCommerce extensions think WooCommerce is present and active.
Version: 9999.0
Requires at least: 4.9
Tested up to: 5.3
*/

And once this dummy plugin is installed, it must be activated of course.

2 Likes

Plugins can easily be renamed, and then your check for active_plugins doesn’t work.
The correct way to check for existence is using function_exists or class_exists.
But you don’t know what every piece of code out in the wild is using.

2 Likes

Over the years I’ve seen plugins check for woocommerce class or if is_woocommerce() or WC() functions exist…tend to be more reliable

2 Likes

The ideal and preferred was is to check if a function or class exists but unfortunately some developers are still relying on the active_plugins check.

The issue we are trying to resolve is for those plugins that check on the active status - maybe @omukiguy and @james can throw more light on this on the best way forward.

1 Like

Really is_plugin_active is not the best way to check, and also don’t work in the frontend (without including wp-admin/includes/plugin.php).

But we will face this problem for each plugin that have “extensions” plugins we are going to fork.
So… why not adding a filter before returning is_plugin_active?
Yes… it’s about changing CP core to help in those situations :slight_smile:

I’ve made no much experiments but it seems to break nothing.

Simone.

1 Like

One of the main issues is that, as CC and WC both use the same woocommerce class, they cannot be installed and activated at the same time. For example, if CC is active and then you try to activate WC, you immediately get a fatal PHP error:

Fatal error: Cannot redeclare wc()

For those plugins looking for the existence of woocommerce/woocommerce.php, the WooCommerce Placeholder code mentioned by @anon95694377 should be all that’s required.

If CC is active, class_exists('woocommerce') will return true.

1 Like

Sure. But if a filter is added to is_plugin_active() can be easily bypassed by CC accessing directly the active_plugins option.

There is no a file existence check in plugins. It’s only done in the loading process of CP in wp-includes/load.php.

1 Like

What would be the process for the “Placeholder” plugin being installed and present before an addon that “requires” it is activated?

Should Classic Commerce assume upon its installation that there will be a need for the placeholder plugin and install it?

If so, how will this sit with end users?

Or should the user be left informed enough to know that there may be a need for the placeholder plugin needs to be present and active?

Just trying to think as an end user rather than a developer here :slight_smile:

3 Likes

Good questions. I’m not really understanding the technical issues, but as an end user the idea of having to install a plugin to make another work is not ideal. Is this a temporary fix or do we see it as a permanent solution?

2 Likes

The intention is that the user doesn’t need to do anything other than install CC. CC will handle the rest. That’s the aim.

But all of this is still at the idea stage and we’ll be carrying out some testing shortly.

4 Likes