I intend to programmatically create a widget area for each product category within WooCommerce.
I currently have the following code in my functions.php
:
function add_widget_areas() {
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => 0,
);
$product_cats = get_terms( $args );
foreach ( $product_cats as $product_cat ) {
register_sidebar(
array(
'name' => 'Filter sidebar -' . $product_cat->name,
'id' => 'filter_sidebar_' . $product_cat->name,
'before_widget' => '<div class="filter-sidebar-' . $product_cat->name . '">',
'after_widget' => '</div>',
'before_title' => '<h2 class="filter-title">',
'after_title' => '</h2>',
)
);
}
}
add_action( 'widgets_init', 'add_widget_areas' );
My problem is that this returns an error: Notice: Trying to get property of non-object in .../functions.php on line 752
(line 752: 'name' => 'Filter sidebar -' . $product_cat->name,
), presumably because the action for registering widget areas fires before WooCommerce has initialised its product categories.
How do I access the product categories this early in order to make this loop work?