Best Selling product widget code in woocommerce

Add these code to your functions.php to include a new widget for best selling products:

function registering_widget() {
register_widget( ‘WC_Widget_best_selling_products’);
}
add_action( ‘widgets_init’, ‘registering_widget’ );

// best selling products

class WC_Widget_best_selling_products extends WC_Widget {
/*** Constructor.***/
public function __construct() {
$this->widget_cssclass    = ‘Best Selling Products’;
$this->widget_description = __( ‘Best Selling Products in store’, ‘woocommerce’ );
$this->widget_id          = ‘woocommerce_best_selling_product’;
$this->widget_name        = __( ‘WooCommerce Best Selling Products’, ‘woocommerce’ );
$args=array(
‘name’ => ‘genre’
);
$output = ‘objects’; // or names
$this->settings           = array(
‘title’  => array(
‘type’  => ‘text’,
‘std’   => __( ‘Best Selling Product’, ‘woocommerce’ ),
‘label’ => __( ‘Title’, ‘woocommerce’ )
),
‘number’ => array(
‘type’  => ‘number’,
‘step’  => 1,
‘min’   => 1,
‘max’   => ”,
‘std’   => 5,
‘label’ => __( ‘Number of taxonomies to show’, ‘woocommerce’ )
)
);
parent::__construct();
}
/*** Output widget. **/
public function widget( $args, $instance ) {
$this->widget_start( $args, $instance );
$number  = ! empty( $instance[‘number’] ) ? absint( $instance[‘number’] ) : $this->settings[‘number’][‘std’];
global $woocommerce_loop;
$args = array(
‘post_type’ => ‘product’,
‘meta_key’ => ‘total_sales’,
‘orderby’ => ‘meta_value_num’,
‘posts_per_page’ =>$number,
);

echo “<div class=’best_sell_pdt’>”;
// set woocommerce columns
$woocommerce_loop[‘columns’] = $columns;

// query database
$products = new WP_Query( $args );

$woocommerce_loop[‘columns’] = $columns;

if ( $products->have_posts() ) : ?>

<?php woocommerce_product_loop_start(); ?>

<?php while ( $products->have_posts() ) : $products->the_post(); ?>

<?php woocommerce_get_template_part( ‘content’, ‘product’ ); ?>

<?php endwhile; // end of the loop. ?>

<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
echo “</div>”;
$this->widget_end( $args, $instance );
}
}