Advertisement
Guest User

Untitled

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