<?php
/**
 * Show options for ordering
 *
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     2.0.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $woocommerce, $wp_query;

if ( !$orderby || $orderby == 'title' ) {
	$orderby = 'menu_order';
}

if ( 1 == $wp_query->found_posts || ! woocommerce_products_will_display() ) {
	return;
}

$taxonomy = get_query_var('taxonomy');
$taxonomy_val = get_query_var($taxonomy);
?>

<form class="woocommerce-ordering" method="get">
	<label><?php _e('Sort by', WFT_DOMAIN); ?>:</label>
	<select name="orderby" class="orderby_ajax">
		<?php
			$catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
				'menu_order' => __( 'Default', 'woocommerce' ),
				'popularity' => __( 'Popularity', 'woocommerce' ),
				'rating'     => __( 'Average rating', 'woocommerce' ),
				'date'       => __( 'Newness', 'woocommerce' ),
				'price'      => __( 'Price: low to high', 'woocommerce' ),
				'price_desc' => __( 'Price: high to low', 'woocommerce' )
			) );

			if ( get_option( 'woocommerce_enable_review_rating' ) == 'no' ) {
				unset( $catalog_orderby['rating'] );
			}

			foreach ( $catalog_orderby as $id => $name ) {
				echo '<option value="' . esc_attr( $id ) . '" ' . selected( $orderby, $id, false ) . '>' . esc_attr( $name ) . '</option>';
			}
		?>
	</select>
	<?php
		// Keep query string vars intact
		foreach ( $_GET as $key => $val ) {
			if ( 'orderby' == $key ) {
				continue;
			}

			if (is_array($val)) {
				foreach($val as $innerVal) {
					echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
				}

			} else {
				echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
			}
		}

	?>

	<input type="hidden" name="<?php echo $taxonomy; ?>" value="<?php echo $taxonomy_val; ?>" />

</form>