/widgets/widget-price_filter.php

https://github.com/bigdawggi/woocommerce · PHP · 172 lines · 108 code · 36 blank · 28 comment · 18 complexity · 47a707aea64baa7aba7b7aa582ce10a8 MD5 · raw file

  1. <?php
  2. /**
  3. * Price Filter Widget and related functions
  4. *
  5. * Generates a range slider to filter products by price
  6. *
  7. * @package WooCommerce
  8. * @category Widgets
  9. * @author WooThemes
  10. */
  11. /**
  12. * Price filter Init
  13. */
  14. add_action('init', 'woocommerce_price_filter_init');
  15. function woocommerce_price_filter_init() {
  16. unset($_SESSION['min_price']);
  17. unset($_SESSION['max_price']);
  18. if (isset($_GET['min_price'])) :
  19. $_SESSION['min_price'] = $_GET['min_price'];
  20. endif;
  21. if (isset($_GET['max_price'])) :
  22. $_SESSION['max_price'] = $_GET['max_price'];
  23. endif;
  24. }
  25. /**
  26. * Price Filter post filter
  27. */
  28. add_filter('loop_shop_post_in', 'woocommerce_price_filter');
  29. function woocommerce_price_filter( $filtered_posts ) {
  30. if (isset($_GET['max_price']) && isset($_GET['min_price'])) :
  31. $matched_products = array();
  32. $matched_products_query = get_posts(array(
  33. 'post_type' => 'product',
  34. 'post_status' => 'publish',
  35. 'posts_per_page' => -1,
  36. 'meta_query' => array(
  37. array(
  38. 'key' => 'price',
  39. 'value' => array( $_GET['min_price'], $_GET['max_price'] ),
  40. 'type' => 'NUMERIC',
  41. 'compare' => 'BETWEEN'
  42. )
  43. )
  44. ));
  45. if ($matched_products_query) :
  46. foreach ($matched_products_query as $product) :
  47. $matched_products[] = $product->ID;
  48. if ($product->post_parent>0) $matched_products[] = $product->post_parent;
  49. endforeach;
  50. endif;
  51. // Filter the id's
  52. if (sizeof($filtered_posts)==0) :
  53. $filtered_posts = $matched_products;
  54. $filtered_posts[] = 0;
  55. else :
  56. $filtered_posts = array_intersect($filtered_posts, $matched_products);
  57. $filtered_posts[] = 0;
  58. endif;
  59. endif;
  60. return (array) $filtered_posts;
  61. }
  62. /**
  63. * Price Filter post Widget
  64. */
  65. class WooCommerce_Widget_Price_Filter extends WP_Widget {
  66. /** Variables to setup the widget. */
  67. var $woo_widget_cssclass;
  68. var $woo_widget_description;
  69. var $woo_widget_idbase;
  70. var $woo_widget_name;
  71. /** constructor */
  72. function WooCommerce_Widget_Price_Filter() {
  73. /* Widget variable settings. */
  74. $this->woo_widget_cssclass = 'widget_price_filter';
  75. $this->woo_widget_description = __( 'Shows a price filter slider in a widget which lets you narrow down the list of shown products when viewing product categories.', 'woothemes' );
  76. $this->woo_widget_idbase = 'woocommerce_price_filter';
  77. $this->woo_widget_name = __('WooCommerce Price Filter', 'woothemes' );
  78. /* Widget settings. */
  79. $widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
  80. /* Create the widget. */
  81. $this->WP_Widget('price_filter', $this->woo_widget_name, $widget_ops);
  82. }
  83. /** @see WP_Widget */
  84. function widget( $args, $instance ) {
  85. extract($args);
  86. if (!is_tax( 'product_cat' ) && !is_post_type_archive('product') && !is_tax( 'product_tag' )) return;
  87. global $_chosen_attributes, $wpdb, $woocommerce;
  88. $title = $instance['title'];
  89. $title = apply_filters('widget_title', $title, $instance, $this->id_base);
  90. echo $before_widget . $before_title . $title . $after_title;
  91. // Remember current filters/search
  92. $fields = '';
  93. if (get_search_query()) $fields = '<input type="hidden" name="s" value="'.get_search_query().'" />';
  94. if (isset($_GET['post_type'])) $fields .= '<input type="hidden" name="post_type" value="'.esc_attr( $_GET['post_type'] ).'" />';
  95. if ($_chosen_attributes) foreach ($_chosen_attributes as $attribute => $value) :
  96. $fields .= '<input type="hidden" name="'.esc_attr( str_replace('pa_', 'filter_', $attribute) ).'" value="'.esc_attr( implode(',', $value) ).'" />';
  97. endforeach;
  98. $min = 0;
  99. $max = ceil($wpdb->get_var("SELECT max(meta_value + 0)
  100. FROM $wpdb->posts
  101. LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id
  102. WHERE meta_key = 'price' AND (
  103. $wpdb->posts.ID IN (".implode(',', $woocommerce->query->layered_nav_product_ids).")
  104. OR (
  105. $wpdb->posts.post_parent IN (".implode(',', $woocommerce->query->layered_nav_product_ids).")
  106. AND $wpdb->posts.post_parent != 0
  107. )
  108. )"));
  109. echo '<form method="get" action="">
  110. <div class="price_slider_wrapper">
  111. <div class="price_slider"></div>
  112. <div class="price_slider_amount">
  113. <button type="submit" class="button">Filter</button>'.__('Price: ', 'woothemes').'<span></span>
  114. <input type="hidden" id="max_price" name="max_price" value="'.esc_attr( $max ).'" />
  115. <input type="hidden" id="min_price" name="min_price" value="'.esc_attr( $min ).'" />
  116. '.$fields.'
  117. </div>
  118. </div>
  119. </form>';
  120. echo $after_widget;
  121. }
  122. /** @see WP_Widget->update */
  123. function update( $new_instance, $old_instance ) {
  124. if (!isset($new_instance['title']) || empty($new_instance['title'])) $new_instance['title'] = __('Filter by price', 'woothemes');
  125. $instance['title'] = strip_tags(stripslashes($new_instance['title']));
  126. return $instance;
  127. }
  128. /** @see WP_Widget->form */
  129. function form( $instance ) {
  130. global $wpdb;
  131. ?>
  132. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'woothemes') ?></label>
  133. <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>
  134. <?php
  135. }
  136. } // class WooCommerce_Widget_Price_Filter