Skip to content

Instantly share code, notes, and snippets.

@MarkPraschan
Created November 17, 2017 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkPraschan/dad06e2d74d1f3537c22beec0fc95c02 to your computer and use it in GitHub Desktop.
Save MarkPraschan/dad06e2d74d1f3537c22beec0fc95c02 to your computer and use it in GitHub Desktop.
Hybrid WooCommerce Product Category Widget (Dropdown PLUS Subcategory List)
<?php
/**
* Adds customized product categories widget with subcategories (when applicable).
*/
class subcat_widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'subcat_widget', // Base ID
esc_html__( 'Custom Categories Widget', 'text_domain' ), // Name
array( 'description' => esc_html__( 'Display categories and sub-categories', 'text_domain' ), ) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
wc_product_dropdown_categories( array( 'show_count' => 0 , 'hide_empty' => 1 ) );
$parentid = get_queried_object_id();
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<ul class="product-categories store-links">';
foreach ( $terms as $term ) {
echo '<li class="cat-item">';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
echo '</div>';
?>
<script type='text/javascript'>
/* <![CDATA[ */
var product_cat_dropdown = jQuery(".dropdown_product_cat");
product_cat_dropdown.change(function() {
if ( product_cat_dropdown.val() !=='' ) {
location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.val();
}
});
/* ]]> */
</script>
<?php
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Product Categories', 'text_domain' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // class subcat_widget
// register subcat_widget widget
function register_subcat_widget() {
register_widget( 'subcat_widget' );
}
add_action( 'widgets_init', 'register_subcat_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment