1 <?php
2 3 4 5 6 7 8 9 10
11
12 if ( ! defined( 'ABSPATH' ) ) exit;
13
14 class WC_Widget_Products extends WC_Widget {
15
16 17 18
19 public function __construct() {
20 $this->widget_cssclass = 'woocommerce widget_products';
21 $this->widget_description = __( 'Display a list of your products on your site.', 'woocommerce' );
22 $this->widget_id = 'woocommerce_products';
23 $this->widget_name = __( 'WooCommerce Products', 'woocommerce' );
24 $this->settings = array(
25 'title' => array(
26 'type' => 'text',
27 'std' => __( 'Products', 'woocommerce' ),
28 'label' => __( 'Title', 'woocommerce' )
29 ),
30 'number' => array(
31 'type' => 'number',
32 'step' => 1,
33 'min' => 1,
34 'max' => '',
35 'std' => 5,
36 'label' => __( 'Number of products to show', 'woocommerce' )
37 ),
38 'show' => array(
39 'type' => 'select',
40 'std' => '',
41 'label' => __( 'Show', 'woocommerce' ),
42 'options' => array(
43 '' => __( 'All Products', 'woocommerce' ),
44 'featured' => __( 'Featured Products', 'woocommerce' ),
45 'onsale' => __( 'On-sale Products', 'woocommerce' ),
46 )
47 ),
48 'orderby' => array(
49 'type' => 'select',
50 'std' => 'date',
51 'label' => __( 'Order by', 'woocommerce' ),
52 'options' => array(
53 'date' => __( 'Date', 'woocommerce' ),
54 'price' => __( 'Price', 'woocommerce' ),
55 'rand' => __( 'Random', 'woocommerce' ),
56 'sales' => __( 'Sales', 'woocommerce' ),
57 )
58 ),
59 'order' => array(
60 'type' => 'select',
61 'std' => 'desc',
62 'label' => _x( 'Order', 'Sorting order', 'woocommerce' ),
63 'options' => array(
64 'asc' => __( 'ASC', 'woocommerce' ),
65 'desc' => __( 'DESC', 'woocommerce' ),
66 )
67 ),
68 'hide_free' => array(
69 'type' => 'checkbox',
70 'std' => 0,
71 'label' => __( 'Hide free products', 'woocommerce' )
72 ),
73 'show_hidden' => array(
74 'type' => 'checkbox',
75 'std' => 0,
76 'label' => __( 'Show hidden products', 'woocommerce' )
77 )
78 );
79 parent::__construct();
80 }
81
82 83 84 85 86 87 88 89 90
91 public function widget( $args, $instance ) {
92
93 if ( $this->get_cached_widget( $args ) )
94 return;
95
96 ob_start();
97 extract( $args );
98
99 $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
100 $number = absint( $instance['number'] );
101 $show = sanitize_title( $instance['show'] );
102 $orderby = sanitize_title( $instance['orderby'] );
103 $order = sanitize_title( $instance['order'] );
104 $show_rating = false;
105
106 $query_args = array(
107 'posts_per_page' => $number,
108 'post_status' => 'publish',
109 'post_type' => 'product',
110 'no_found_rows' => 1,
111 'order' => $order == 'asc' ? 'asc' : 'desc'
112 );
113
114 $query_args['meta_query'] = array();
115
116 if ( empty( $instance['show_hidden'] ) ) {
117 $query_args['meta_query'][] = WC()->query->visibility_meta_query();
118 $query_args['post_parent'] = 0;
119 }
120
121 if ( ! empty( $instance['hide_free'] ) ) {
122 $query_args['meta_query'][] = array(
123 'key' => '_price',
124 'value' => 0,
125 'compare' => '>',
126 'type' => 'DECIMAL',
127 );
128 }
129
130 $query_args['meta_query'][] = WC()->query->stock_status_meta_query();
131 $query_args['meta_query'] = array_filter( $query_args['meta_query'] );
132
133 switch ( $show ) {
134 case 'featured' :
135 $query_args['meta_query'][] = array(
136 'key' => '_featured',
137 'value' => 'yes'
138 );
139 break;
140 case 'onsale' :
141 $product_ids_on_sale = wc_get_product_ids_on_sale();
142 $product_ids_on_sale[] = 0;
143 $query_args['post__in'] = $product_ids_on_sale;
144 break;
145 }
146
147 switch ( $orderby ) {
148 case 'price' :
149 $query_args['meta_key'] = '_price';
150 $query_args['orderby'] = 'meta_value_num';
151 break;
152 case 'rand' :
153 $query_args['orderby'] = 'rand';
154 break;
155 case 'sales' :
156 $query_args['meta_key'] = 'total_sales';
157 $query_args['orderby'] = 'meta_value_num';
158 break;
159 default :
160 $query_args['orderby'] = 'date';
161 }
162
163 $r = new WP_Query( $query_args );
164
165 if ( $r->have_posts() ) {
166
167 echo $before_widget;
168
169 if ( $title )
170 echo $before_title . $title . $after_title;
171
172 echo '<ul class="product_list_widget">';
173
174 while ( $r->have_posts()) {
175 $r->the_post();
176 wc_get_template( 'content-widget-product.php', array( 'show_rating' => $show_rating ) );
177 }
178
179 echo '</ul>';
180
181 echo $after_widget;
182 }
183
184 wp_reset_postdata();
185
186 $content = ob_get_clean();
187
188 echo $content;
189
190 $this->cache_widget( $args, $content );
191 }
192 }
193
194 register_widget( 'WC_Widget_Products' );