WooCommerce comes with a Layered Navigation Widget that’s pretty cool. If you’re unfamiliar:
Layered Navigation is a powerful widget which empowers your users to drill down through your catalog of products via attributes, effectively helping them find what they’re looking for quicker.
On a current project, that’s exactly what was needed. However, you have to add each individual attribute as a separate widget in the backend. Since I don’t want my clients having more work than they need to get ramped up (because I empower them to add their own content instead of paying me), I wanted a solution that would automatically add the relevant widgets based on the products being viewed. Turns out, the_widget()
is our friend.
In my sidebar template, I’ve got the following running before my call to the dynamic sidebar. You can place it in whatever order you like there.
$attribute->attribute_label,
'attribute' => $attribute->attribute_name,
'query_type' => 'and',
'display_type' => 'list'
)
);
}
} ?>
This calls widgets based on all the attributes belonging to the current query, so no one has to add a widget each time a new attribute is created.
The only ‘gotcha’: you appear to need at least one instance of the actual widget in that sidebar to make things work—placed via the WordPress admin area, as usual. I created a new attribute called ‘placeholder’ (without any products assigned to it), and use that as the attribute selected in the widget. Since WooCommerce will hide these widgets when the attribute isn’t being used, this placeholder widget never shows, but triggers whatever action is necessary.
I hope this helps you! If there’s a real use for it, I’ll consider making this into a plugin, or request some sort of addition into WooCommerce.