Move all query string vars to form in price widget using wc_query_string_form_fields

Closes #11593

@claudiosmweb @justinshreve look fine to you?
This commit is contained in:
Mike Jolley 2016-08-04 13:37:39 +01:00
parent 75895f71f9
commit 644cf79866
2 changed files with 12 additions and 43 deletions

View File

@ -344,10 +344,12 @@ function wc_product_post_class( $classes, $class = '', $post_id = '' ) {
* @param array $exclude Keys to exclude. * @param array $exclude Keys to exclude.
* @param string $current_key Current key we are outputting. * @param string $current_key Current key we are outputting.
*/ */
function wc_query_string_form_fields( $values = null, $exclude = array(), $current_key = '' ) { function wc_query_string_form_fields( $values = null, $exclude = array(), $current_key = '', $return = false ) {
if ( is_null( $values ) ) { if ( is_null( $values ) ) {
$values = $_GET; $values = $_GET;
} }
$html = '';
foreach ( $values as $key => $value ) { foreach ( $values as $key => $value ) {
if ( in_array( $key, $exclude, true ) ) { if ( in_array( $key, $exclude, true ) ) {
continue; continue;
@ -356,11 +358,17 @@ function wc_query_string_form_fields( $values = null, $exclude = array(), $curre
$key = $current_key . '[' . $key . ']'; $key = $current_key . '[' . $key . ']';
} }
if ( is_array( $value ) ) { if ( is_array( $value ) ) {
wc_query_string_form_fields( $value, $exclude, $key ); $html .= wc_query_string_form_fields( $value, $exclude, $key, true );
} else { } else {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />'; $html .= '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />';
} }
} }
if ( $return ) {
return $html;
} else {
echo $html;
}
} }
/** Template pages ********************************************************/ /** Template pages ********************************************************/

View File

@ -68,45 +68,6 @@ class WC_Widget_Price_Filter extends WC_Widget {
wp_enqueue_script( 'wc-price-slider' ); wp_enqueue_script( 'wc-price-slider' );
// Remember current filters/search
$fields = '';
if ( get_search_query() ) {
$fields .= '<input type="hidden" name="s" value="' . get_search_query() . '" />';
}
if ( ! empty( $_GET['post_type'] ) ) {
$fields .= '<input type="hidden" name="post_type" value="' . esc_attr( $_GET['post_type'] ) . '" />';
}
if ( ! empty ( $_GET['product_cat'] ) ) {
$fields .= '<input type="hidden" name="product_cat" value="' . esc_attr( $_GET['product_cat'] ) . '" />';
}
if ( ! empty( $_GET['product_tag'] ) ) {
$fields .= '<input type="hidden" name="product_tag" value="' . esc_attr( $_GET['product_tag'] ) . '" />';
}
if ( ! empty( $_GET['orderby'] ) ) {
$fields .= '<input type="hidden" name="orderby" value="' . esc_attr( $_GET['orderby'] ) . '" />';
}
if ( ! empty( $_GET['min_rating'] ) ) {
$fields .= '<input type="hidden" name="min_rating" value="' . esc_attr( $_GET['min_rating'] ) . '" />';
}
if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) {
foreach ( $_chosen_attributes as $attribute => $data ) {
$taxonomy_filter = 'filter_' . str_replace( 'pa_', '', $attribute );
$fields .= '<input type="hidden" name="' . esc_attr( $taxonomy_filter ) . '" value="' . esc_attr( implode( ',', $data['terms'] ) ) . '" />';
if ( 'or' == $data['query_type'] ) {
$fields .= '<input type="hidden" name="' . esc_attr( str_replace( 'pa_', 'query_type_', $attribute ) ) . '" value="or" />';
}
}
}
// Find min and max price in current result set // Find min and max price in current result set
$prices = $this->get_filtered_price(); $prices = $this->get_filtered_price();
$min = floor( $prices->min_price ); $min = floor( $prices->min_price );
@ -152,7 +113,7 @@ class WC_Widget_Price_Filter extends WC_Widget {
<div class="price_label" style="display:none;"> <div class="price_label" style="display:none;">
' . __( 'Price:', 'woocommerce' ) . ' <span class="from"></span> &mdash; <span class="to"></span> ' . __( 'Price:', 'woocommerce' ) . ' <span class="from"></span> &mdash; <span class="to"></span>
</div> </div>
' . $fields . ' ' . wc_query_string_form_fields( null, array( 'min_price', 'max_price' ), '', true ) . '
<div class="clear"></div> <div class="clear"></div>
</div> </div>
</div> </div>