Extend Woocommerce Widget Class – WC_Widget_Product_Categories

I’m trying to extend the woocommerce class used to create the product categories widget in my own plugin which adds a new taxonomy for woo commerce products called “departments”.

When I extend WP_Widget everything works just fine and I see a new widget that I can add in Appearance->Widgets. However, when I’m trying to extend WC_Widget or WC_Widget_Product_Categories I simply don’t see a new widget, even though I’m not getting any php errors.

Here’s what I have which doesn’t work:

//get the base classes
if(!class_exists(‘WC_Widget_Product_Categories’)) {
// require_once plugin_dir_path( __FILE__ ) . ‘/_inc/WC_Widget_Product_Categories.php’;
require_once ABSPATH . ‘wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-widget.php’;
require_once ABSPATH . ‘wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-product-categories.php’;

* Product departments widget class.
* @extends WC_Widget_Product_Categories
class WC_Widget_Product_Departments extends WC_Widget_Product_Categories {
* Constructor.
public function __construct() {

$this->widget_cssclass = ‘woocommerce widget_product_departments’;
$this->widget_description = __( ‘A list or dropdown of product departments.’, ‘woocommerce’ );
$this->widget_id = ‘woocommerce_product_departments’;
$this->widget_name = __( ‘Shop Departments’, ‘woocommerce’ );

and in my main plugin file:

add_action( ‘widgets_init’, ‘override_woocommerce_widgets’, 15 );

function override_woocommerce_widgets() {
register_widget( ‘WC_Widget_Product_Departments’ );

However, this does work:

class WC_Widget_Product_Departments extends WC_Widget

What am I doing wrong if I want to extend the current widget categories class?

Read more here:: Extend Woocommerce Widget Class – WC_Widget_Product_Categories

Leave a Reply

Your email address will not be published. Required fields are marked *