PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/widgets/class-wc-widget-price-filter.php

https://gitlab.com/CarlCDavid/woocommerce
PHP | 182 lines | 133 code | 24 blank | 25 comment | 20 complexity | dfff963b7605e39c659b567e91a31fe9 MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Price Filter Widget and related functions
  7. *
  8. * Generates a range slider to filter products by price.
  9. *
  10. * @author WooThemes
  11. * @category Widgets
  12. * @package WooCommerce/Widgets
  13. * @version 2.3.0
  14. * @extends WC_Widget
  15. */
  16. class WC_Widget_Price_Filter extends WC_Widget {
  17. /**
  18. * Constructor
  19. */
  20. public function __construct() {
  21. $this->widget_cssclass = 'woocommerce widget_price_filter';
  22. $this->widget_description = __( 'Shows a price filter slider in a widget which lets you narrow down the list of shown products when viewing product categories.', 'woocommerce' );
  23. $this->widget_id = 'woocommerce_price_filter';
  24. $this->widget_name = __( 'WooCommerce Price Filter', 'woocommerce' );
  25. $this->settings = array(
  26. 'title' => array(
  27. 'type' => 'text',
  28. 'std' => __( 'Filter by price', 'woocommerce' ),
  29. 'label' => __( 'Title', 'woocommerce' )
  30. )
  31. );
  32. parent::__construct();
  33. }
  34. /**
  35. * widget function.
  36. *
  37. * @see WP_Widget
  38. *
  39. * @param array $args
  40. * @param array $instance
  41. *
  42. * @return void
  43. */
  44. public function widget( $args, $instance ) {
  45. global $_chosen_attributes, $wpdb, $wp;
  46. if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
  47. return;
  48. }
  49. if ( sizeof( WC()->query->unfiltered_product_ids ) == 0 ) {
  50. return; // None shown - return
  51. }
  52. $min_price = isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : '';
  53. $max_price = isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : '';
  54. wp_enqueue_script( 'wc-price-slider' );
  55. // Remember current filters/search
  56. $fields = '';
  57. if ( get_search_query() ) {
  58. $fields .= '<input type="hidden" name="s" value="' . get_search_query() . '" />';
  59. }
  60. if ( ! empty( $_GET['post_type'] ) ) {
  61. $fields .= '<input type="hidden" name="post_type" value="' . esc_attr( $_GET['post_type'] ) . '" />';
  62. }
  63. if ( ! empty ( $_GET['product_cat'] ) ) {
  64. $fields .= '<input type="hidden" name="product_cat" value="' . esc_attr( $_GET['product_cat'] ) . '" />';
  65. }
  66. if ( ! empty( $_GET['product_tag'] ) ) {
  67. $fields .= '<input type="hidden" name="product_tag" value="' . esc_attr( $_GET['product_tag'] ) . '" />';
  68. }
  69. if ( ! empty( $_GET['orderby'] ) ) {
  70. $fields .= '<input type="hidden" name="orderby" value="' . esc_attr( $_GET['orderby'] ) . '" />';
  71. }
  72. if ( $_chosen_attributes ) {
  73. foreach ( $_chosen_attributes as $attribute => $data ) {
  74. $taxonomy_filter = 'filter_' . str_replace( 'pa_', '', $attribute );
  75. $fields .= '<input type="hidden" name="' . esc_attr( $taxonomy_filter ) . '" value="' . esc_attr( implode( ',', $data['terms'] ) ) . '" />';
  76. if ( 'or' == $data['query_type'] ) {
  77. $fields .= '<input type="hidden" name="' . esc_attr( str_replace( 'pa_', 'query_type_', $attribute ) ) . '" value="or" />';
  78. }
  79. }
  80. }
  81. if ( 0 === sizeof( WC()->query->layered_nav_product_ids ) ) {
  82. $min = floor( $wpdb->get_var(
  83. $wpdb->prepare('
  84. SELECT min(meta_value + 0)
  85. FROM %1$s
  86. LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
  87. WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price', '_min_variation_price' ) ) ) . '")
  88. AND meta_value != ""
  89. ', $wpdb->posts, $wpdb->postmeta )
  90. ) );
  91. $max = ceil( $wpdb->get_var(
  92. $wpdb->prepare('
  93. SELECT max(meta_value + 0)
  94. FROM %1$s
  95. LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
  96. WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) . '")
  97. ', $wpdb->posts, $wpdb->postmeta, '_price' )
  98. ) );
  99. } else {
  100. $min = floor( $wpdb->get_var(
  101. $wpdb->prepare('
  102. SELECT min(meta_value + 0)
  103. FROM %1$s
  104. LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
  105. WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price', '_min_variation_price' ) ) ) . '")
  106. AND meta_value != ""
  107. AND (
  108. %1$s.ID IN (' . implode( ',', array_map( 'absint', WC()->query->layered_nav_product_ids ) ) . ')
  109. OR (
  110. %1$s.post_parent IN (' . implode( ',', array_map( 'absint', WC()->query->layered_nav_product_ids ) ) . ')
  111. AND %1$s.post_parent != 0
  112. )
  113. )
  114. ', $wpdb->posts, $wpdb->postmeta
  115. ) ) );
  116. $max = ceil( $wpdb->get_var(
  117. $wpdb->prepare('
  118. SELECT max(meta_value + 0)
  119. FROM %1$s
  120. LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
  121. WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) . '")
  122. AND (
  123. %1$s.ID IN (' . implode( ',', array_map( 'absint', WC()->query->layered_nav_product_ids ) ) . ')
  124. OR (
  125. %1$s.post_parent IN (' . implode( ',', array_map( 'absint', WC()->query->layered_nav_product_ids ) ) . ')
  126. AND %1$s.post_parent != 0
  127. )
  128. )
  129. ', $wpdb->posts, $wpdb->postmeta
  130. ) ) );
  131. }
  132. if ( $min == $max ) {
  133. return;
  134. }
  135. $this->widget_start( $args, $instance );
  136. if ( '' == get_option( 'permalink_structure' ) ) {
  137. $form_action = remove_query_arg( array( 'page', 'paged' ), add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
  138. } else {
  139. $form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( trailingslashit( $wp->request ) ) );
  140. }
  141. echo '<form method="get" action="' . esc_url( $form_action ) . '">
  142. <div class="price_slider_wrapper">
  143. <div class="price_slider" style="display:none;"></div>
  144. <div class="price_slider_amount">
  145. <input type="text" id="min_price" name="min_price" value="' . esc_attr( $min_price ) . '" data-min="' . esc_attr( apply_filters( 'woocommerce_price_filter_widget_amount', $min ) ) . '" placeholder="' . __('Min price', 'woocommerce' ) . '" />
  146. <input type="text" id="max_price" name="max_price" value="' . esc_attr( $max_price ) . '" data-max="' . esc_attr( apply_filters( 'woocommerce_price_filter_widget_amount', $max ) ) . '" placeholder="' . __( 'Max price', 'woocommerce' ) . '" />
  147. <button type="submit" class="button">' . __( 'Filter', 'woocommerce' ) . '</button>
  148. <div class="price_label" style="display:none;">
  149. ' . __( 'Price:', 'woocommerce' ) . ' <span class="from"></span> &mdash; <span class="to"></span>
  150. </div>
  151. ' . $fields . '
  152. <div class="clear"></div>
  153. </div>
  154. </div>
  155. </form>';
  156. $this->widget_end( $args );
  157. }
  158. }