'bike-shop-widget bike_shop_category_products', 'description' => esc_html__( 'Display products in slider from selected category.', 'bike-shop' ), 'customize_selective_refresh' => true, ); parent::__construct( 'bike_shop_product_slider', esc_html__( 'Bike Shop: Display Products in slider by Category', 'bike-shop' ), $widget_ops ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $fields = array( 'productslider_title' => array( 'bike_shop_widgets_name' => 'productslider_title', 'bike_shop_widgets_title' => esc_html__( 'Widget title', 'bike-shop' ), 'bike_shop_widgets_field_type' => 'text' ), 'productslider_category_id' => array( 'bike_shop_widgets_name' => 'productslider_category_id', 'bike_shop_widgets_title' => esc_html__( 'Product Category', 'bike-shop' ), 'bike_shop_widgets_field_type' => 'select', 'bike_shop_widgets_field_options' => bike_shop_product_categories_dropdown() ), 'productslider_post_count' => array( 'bike_shop_widgets_name' => 'productslider_post_count', 'bike_shop_widgets_title' => esc_html__( 'Post Count', 'bike-shop' ), 'bike_shop_widgets_default' => '5', 'bike_shop_widgets_field_type' => 'number' ), 'category_product_enable_slider' => array( 'bike_shop_widgets_name' => 'category_product_enable_slider', 'bike_shop_widgets_title' => esc_html__( 'Enable Slider', 'bike-shop' ), 'bike_shop_widgets_default' => true, 'bike_shop_widgets_field_type' => 'checkbox' ), ); return $fields; } /** * 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 ) { extract( $args ); if( empty( $instance ) ) { return ; } $bike_shop_widget_title = empty( $instance['productslider_title'] ) ? '' : $instance['productslider_title']; $bike_shop_product_cat_id = empty( $instance['productslider_category_id'] ) ? '' : $instance['productslider_category_id']; $bike_shop_post_count = empty( $instance['productslider_post_count'] ) ? '5' : $instance['productslider_post_count']; $bike_shop_post_enable_slider = empty( $instance['category_product_enable_slider'] ) ? '' : $instance['category_product_enable_slider']; echo $before_widget; ?>
'product', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $bike_shop_product_cat_id, ), ), 'posts_per_page'=> absint( $bike_shop_post_count ), ); $category_product_cat_query = new WP_Query( $category_product_cat_args ); if ( $category_product_cat_query -> have_posts() ) : ?>

have_posts() ) : $category_product_cat_query -> the_post(); wc_get_template_part( 'content', 'product' ); endwhile; ?>
widget_fields(); // Loop through fields foreach ( $widget_fields as $widget_field ) { extract( $widget_field ); // Use helper function to get updated field values $instance[$bike_shop_widgets_name] = bike_shop_widgets_updated_field_value( $widget_field, $new_instance[$bike_shop_widgets_name] ); } return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. * * @uses bike_shop_widgets_show_widget_field() */ public function form( $instance ) { $widget_fields = $this->widget_fields(); // Loop through fields foreach ( $widget_fields as $widget_field ) { // Make array elements available as variables extract( $widget_field ); $bike_shop_widgets_field_value = !empty( $instance[$bike_shop_widgets_name] ) ? $instance[$bike_shop_widgets_name]: ''; bike_shop_widgets_show_widget_field( $this, $widget_field, $bike_shop_widgets_field_value ); } } }