'bearded-products-widget', 'description' => __('Show Latest Woocommerce Products. Best use in homepage widget.', 'bearded') ); $control_ops = array(); $this->WP_Widget('bearded-products', __('Bearded Products', 'bearded'), $widget_ops, $control_ops); } function widget( $args, $instance ) { extract($args); $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); $number = absint( $instance['numberposts'] ); $type = sanitize_title( $instance['type'] ); $orderby = sanitize_title( $instance['orderby'] ); $order = sanitize_title( $instance['order'] ); $show_hidden = $instance['show_hidden']; $hide_free = $instance['hide_free']; $query_args = array( 'posts_per_page' => $number, 'post_status' => 'publish', 'post_type' => 'product', 'no_found_rows' => 1, 'order' => $order == 'asc' ? 'asc' : 'desc' ); $query_args['meta_query'] = array(); if ( $show_hidden ) { $query_args['meta_query'][] = WC()->query->visibility_meta_query(); $query_args['post_parent'] = 0; } if ( !$hide_free ) { $query_args['meta_query'][] = array( 'key' => '_price', 'value' => 0, 'compare' => '>', 'type' => 'DECIMAL', ); } $query_args['meta_query'][] = WC()->query->stock_status_meta_query(); $query_args['meta_query'] = array_filter( $query_args['meta_query'] ); switch ( $type ) { case 'featured' : $query_args['meta_query'][] = array( 'key' => '_featured', 'value' => 'yes' ); break; case 'onsale' : $product_ids_on_sale = wc_get_product_ids_on_sale(); $product_ids_on_sale[] = 0; $query_args['post__in'] = $product_ids_on_sale; break; } switch ( $orderby ) { case 'price' : $query_args['meta_key'] = '_price'; $query_args['orderby'] = 'meta_value_num'; break; case 'rand' : $query_args['orderby'] = 'rand'; break; case 'sales' : $query_args['meta_key'] = 'total_sales'; $query_args['orderby'] = 'meta_value_num'; break; default : $query_args['orderby'] = 'date'; } $r = new WP_Query( $query_args ); if ( $r->have_posts() ) { echo $before_widget; ?>
have_posts()) { $r->the_post(); wc_get_template( 'content-product.php' ); } ?>
'Latest Products', 'type' => '', 'numberposts' => 4, 'orderby' => 'date', 'order' => 'ASC', 'hide_free' => false, 'show_hidden' => false, ); $instance = wp_parse_args( (array) $instance, $defaults ); /* Build our form ---------------------------------------------------------------*/ ?>