0
votes

I'm trying to extend a WooCommerce class that concerns the Wordpress admin interface. I'm currently trying to change one of the functions inside the class by extending it. I'm attempting this:

if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
  add_action('plugins_loaded', "overwrite_woocommerce");
}

function overwrite_woocommerce() {
  class Extend_WC_Admin_Permalink_Settings extends WC_Admin_Permalink_Settings{
    public function settings_save() {
        // Stuff that happens
    }
  }
}

But after trying both the plugins_loaded and admin_init hooks, neither seem to work. Editing the WooCommerce class directly in the plugin folder achieves the outcome I'm looking for.

I'm trying to remove these lines in particular from the function:

if ( '/%product_cat%/' === trailingslashit( $product_base ) ) {
    $product_base = '/' . _x( 'product', 'slug', 'woocommerce' ) . $product_base;
}

And this function resolves with

update_option( 'woocommerce_permalinks', $permalinks );

Ultimately I could use the update_option hooks, the problem being that WooCommerce forces "/product/" as a base if "/%product_cat%/" is set as a custom URL base, which leaves me with no way of knowing if the original value was "/%product_cat%/" to begin with as it's set before the hook fires. Hence why I want to extend the class in the first place.

1

1 Answers

-2
votes

I think that you won't be able to do that. I don't know if you are coming from Prestashop or Joomla when overwriting core/modules file is common but in WP your best shot is to analyze the part you are trying to overwrite and see if it has any hooks to make it work as you want.

If this answer is not enough then post the part you are trying to change and we will try to tell you if there's another way of doing that