2012-05-26 16:25:07 +00:00
|
|
|
<?php
|
2017-11-07 08:55:27 +00:00
|
|
|
/**
|
|
|
|
* Layered nav widget
|
|
|
|
*
|
|
|
|
* @package WooCommerce/Widgets
|
|
|
|
*/
|
2014-11-19 17:12:56 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Layered Navigation Widget.
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
2014-11-15 01:12:59 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Widgets
|
2016-02-09 14:41:17 +00:00
|
|
|
* @version 2.6.0
|
2014-11-15 01:12:59 +00:00
|
|
|
* @extends WC_Widget
|
2012-05-26 16:25:07 +00:00
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
class WC_Widget_Layered_Nav extends WC_Widget {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Constructor.
|
2012-08-14 17:37:50 +00:00
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public function __construct() {
|
2017-07-10 13:36:47 +00:00
|
|
|
$this->widget_cssclass = 'woocommerce widget_layered_nav woocommerce-widget-layered-nav';
|
2017-08-25 11:07:17 +00:00
|
|
|
$this->widget_description = __( 'Display a list of attributes to filter products in your store.', 'woocommerce' );
|
2013-05-24 15:51:58 +00:00
|
|
|
$this->widget_id = 'woocommerce_layered_nav';
|
2017-08-25 11:07:17 +00:00
|
|
|
$this->widget_name = __( 'Filter Products by Attribute', 'woocommerce' );
|
2013-06-20 11:05:07 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-06 18:58:38 +00:00
|
|
|
* Updates a particular instance of a widget.
|
2013-06-20 11:05:07 +00:00
|
|
|
*
|
|
|
|
* @see WP_Widget->update
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2017-11-07 08:55:27 +00:00
|
|
|
* @param array $new_instance New Instance.
|
|
|
|
* @param array $old_instance Old Instance.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2013-06-20 11:05:07 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function update( $new_instance, $old_instance ) {
|
|
|
|
$this->init_settings();
|
|
|
|
return parent::update( $new_instance, $old_instance );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-06 18:58:38 +00:00
|
|
|
* Outputs the settings update form.
|
2013-06-20 11:05:07 +00:00
|
|
|
*
|
|
|
|
* @see WP_Widget->form
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2017-11-07 08:55:27 +00:00
|
|
|
* @param array $instance Instance.
|
2013-06-20 11:05:07 +00:00
|
|
|
*/
|
|
|
|
public function form( $instance ) {
|
|
|
|
$this->init_settings();
|
2013-11-28 13:12:08 +00:00
|
|
|
parent::form( $instance );
|
2013-06-20 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Init settings after post types are registered.
|
2013-06-20 11:05:07 +00:00
|
|
|
*/
|
|
|
|
public function init_settings() {
|
2014-11-15 01:12:59 +00:00
|
|
|
$attribute_array = array();
|
2013-09-12 13:41:02 +00:00
|
|
|
$attribute_taxonomies = wc_get_attribute_taxonomies();
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2016-06-06 16:24:31 +00:00
|
|
|
if ( ! empty( $attribute_taxonomies ) ) {
|
2014-11-15 01:12:59 +00:00
|
|
|
foreach ( $attribute_taxonomies as $tax ) {
|
|
|
|
if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax->attribute_name ) ) ) {
|
|
|
|
$attribute_array[ $tax->attribute_name ] = $tax->attribute_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->settings = array(
|
|
|
|
'title' => array(
|
2013-05-24 15:51:58 +00:00
|
|
|
'type' => 'text',
|
|
|
|
'std' => __( 'Filter by', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Title', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
|
|
|
'attribute' => array(
|
2014-11-15 01:12:59 +00:00
|
|
|
'type' => 'select',
|
|
|
|
'std' => '',
|
|
|
|
'label' => __( 'Attribute', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'options' => $attribute_array,
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
|
|
|
'display_type' => array(
|
2014-11-15 01:12:59 +00:00
|
|
|
'type' => 'select',
|
|
|
|
'std' => 'list',
|
|
|
|
'label' => __( 'Display type', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
'options' => array(
|
2014-11-15 01:12:59 +00:00
|
|
|
'list' => __( 'List', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'dropdown' => __( 'Dropdown', 'woocommerce' ),
|
|
|
|
),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
|
|
|
'query_type' => array(
|
2014-11-15 01:12:59 +00:00
|
|
|
'type' => 'select',
|
|
|
|
'std' => 'and',
|
|
|
|
'label' => __( 'Query type', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
'options' => array(
|
|
|
|
'and' => __( 'AND', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'or' => __( 'OR', 'woocommerce' ),
|
|
|
|
),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
|
|
|
);
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 17:37:50 +00:00
|
|
|
/**
|
2016-01-06 18:58:38 +00:00
|
|
|
* Output widget.
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
|
|
|
* @see WP_Widget
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2017-11-07 08:55:27 +00:00
|
|
|
* @param array $args Arguments.
|
|
|
|
* @param array $instance Instance.
|
2012-08-14 17:37:50 +00:00
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public function widget( $args, $instance ) {
|
2014-11-15 01:12:59 +00:00
|
|
|
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
|
2012-08-28 15:18:16 +00:00
|
|
|
return;
|
2014-11-15 01:12:59 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-10 10:19:32 +00:00
|
|
|
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
|
|
|
|
$taxonomy = isset( $instance['attribute'] ) ? wc_attribute_taxonomy_name( $instance['attribute'] ) : $this->settings['attribute']['std'];
|
|
|
|
$query_type = isset( $instance['query_type'] ) ? $instance['query_type'] : $this->settings['query_type']['std'];
|
|
|
|
$display_type = isset( $instance['display_type'] ) ? $instance['display_type'] : $this->settings['display_type']['std'];
|
2012-05-26 16:25:07 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
if ( ! taxonomy_exists( $taxonomy ) ) {
|
2012-08-28 15:18:16 +00:00
|
|
|
return;
|
2014-11-15 01:12:59 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$get_terms_args = array( 'hide_empty' => '1' );
|
2013-08-19 13:31:48 +00:00
|
|
|
|
2013-09-12 13:41:02 +00:00
|
|
|
$orderby = wc_attribute_orderby( $taxonomy );
|
2013-08-19 13:31:48 +00:00
|
|
|
|
|
|
|
switch ( $orderby ) {
|
|
|
|
case 'name' :
|
|
|
|
$get_terms_args['orderby'] = 'name';
|
|
|
|
$get_terms_args['menu_order'] = false;
|
|
|
|
break;
|
|
|
|
case 'id' :
|
|
|
|
$get_terms_args['orderby'] = 'id';
|
|
|
|
$get_terms_args['order'] = 'ASC';
|
|
|
|
$get_terms_args['menu_order'] = false;
|
|
|
|
break;
|
|
|
|
case 'menu_order' :
|
|
|
|
$get_terms_args['menu_order'] = 'ASC';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$terms = get_terms( $taxonomy, $get_terms_args );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
if ( 0 === count( $terms ) ) {
|
2016-02-09 14:41:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-06-08 13:39:24 +00:00
|
|
|
switch ( $orderby ) {
|
|
|
|
case 'name_num' :
|
|
|
|
usort( $terms, '_wc_get_product_terms_name_num_usort_callback' );
|
|
|
|
break;
|
|
|
|
case 'parent' :
|
|
|
|
usort( $terms, '_wc_get_product_terms_parent_usort_callback' );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
ob_start();
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
$this->widget_start( $args, $instance );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( 'dropdown' === $display_type ) {
|
2017-07-10 13:36:47 +00:00
|
|
|
wp_enqueue_script( 'selectWoo' );
|
2017-02-24 08:18:38 +00:00
|
|
|
wp_enqueue_style( 'select2' );
|
2016-02-09 14:41:17 +00:00
|
|
|
$found = $this->layered_nav_dropdown( $terms, $taxonomy, $query_type );
|
|
|
|
} else {
|
|
|
|
$found = $this->layered_nav_list( $terms, $taxonomy, $query_type );
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
$this->widget_end( $args );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Force found when option is selected - do not force found on taxonomy attributes.
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( ! is_tax() && is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) {
|
|
|
|
$found = true;
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( ! $found ) {
|
|
|
|
ob_end_clean();
|
|
|
|
} else {
|
2017-11-07 08:55:27 +00:00
|
|
|
echo ob_get_clean(); // @codingStandardsIgnoreLine
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
/**
|
|
|
|
* Return the currently viewed taxonomy name.
|
2017-11-07 08:55:27 +00:00
|
|
|
*
|
2016-02-09 14:41:17 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_current_taxonomy() {
|
|
|
|
return is_tax() ? get_queried_object()->taxonomy : '';
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
/**
|
|
|
|
* Return the currently viewed term ID.
|
2017-11-07 08:55:27 +00:00
|
|
|
*
|
2016-02-09 14:41:17 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function get_current_term_id() {
|
|
|
|
return absint( is_tax() ? get_queried_object()->term_id : 0 );
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
/**
|
|
|
|
* Return the currently viewed term slug.
|
2017-11-07 08:55:27 +00:00
|
|
|
*
|
2016-02-09 14:41:17 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function get_current_term_slug() {
|
|
|
|
return absint( is_tax() ? get_queried_object()->slug : 0 );
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
/**
|
|
|
|
* Show dropdown layered nav.
|
2017-11-07 08:55:27 +00:00
|
|
|
*
|
|
|
|
* @param array $terms Terms.
|
|
|
|
* @param string $taxonomy Taxonomy.
|
|
|
|
* @param string $query_type Query Type.
|
2016-02-09 14:41:17 +00:00
|
|
|
* @return bool Will nav display?
|
|
|
|
*/
|
|
|
|
protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) {
|
2017-07-10 13:36:47 +00:00
|
|
|
global $wp;
|
2016-02-09 14:41:17 +00:00
|
|
|
$found = false;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( $taxonomy !== $this->get_current_taxonomy() ) {
|
2016-06-15 18:20:48 +00:00
|
|
|
$term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type );
|
2017-07-11 10:31:02 +00:00
|
|
|
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
|
2016-02-09 14:41:17 +00:00
|
|
|
$taxonomy_filter_name = str_replace( 'pa_', '', $taxonomy );
|
2016-10-05 18:37:53 +00:00
|
|
|
$taxonomy_label = wc_attribute_label( $taxonomy );
|
|
|
|
$any_label = apply_filters( 'woocommerce_layered_nav_any_label', sprintf( __( 'Any %s', 'woocommerce' ), $taxonomy_label ), $taxonomy_label, $taxonomy );
|
2017-07-10 13:36:47 +00:00
|
|
|
$multiple = 'or' === $query_type;
|
|
|
|
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-07-10 13:36:47 +00:00
|
|
|
if ( '' === get_option( 'permalink_structure' ) ) {
|
|
|
|
$form_action = remove_query_arg( array( 'page', 'paged' ), add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
|
2017-02-24 08:18:38 +00:00
|
|
|
} else {
|
2017-07-10 13:36:47 +00:00
|
|
|
$form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( trailingslashit( $wp->request ) ) );
|
2017-02-24 08:18:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 13:36:47 +00:00
|
|
|
echo '<form method="get" action="' . esc_url( $form_action ) . '" class="woocommerce-widget-layered-nav-dropdown">';
|
|
|
|
echo '<select class="woocommerce-widget-layered-nav-dropdown dropdown_layered_nav_' . esc_attr( $taxonomy_filter_name ) . '"' . ( $multiple ? 'multiple="multiple"' : '' ) . '>';
|
2016-10-05 17:45:09 +00:00
|
|
|
echo '<option value="">' . esc_html( $any_label ) . '</option>';
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
foreach ( $terms as $term ) {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// If on a term page, skip that term in widget list.
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( $term->term_id === $this->get_current_term_id() ) {
|
|
|
|
continue;
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Get count based on current view.
|
2016-11-22 18:48:39 +00:00
|
|
|
$option_is_set = in_array( $term->slug, $current_values );
|
|
|
|
$count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Only show options with count > 0.
|
2016-02-10 10:02:50 +00:00
|
|
|
if ( 0 < $count ) {
|
|
|
|
$found = true;
|
2016-11-22 18:48:39 +00:00
|
|
|
} elseif ( 0 === $count && ! $option_is_set ) {
|
2016-02-10 10:02:50 +00:00
|
|
|
continue;
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( $option_is_set, true, false ) . '>' . esc_html( $term->name ) . '</option>';
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
echo '</select>';
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-07-10 13:36:47 +00:00
|
|
|
if ( $multiple ) {
|
2017-11-07 08:56:36 +00:00
|
|
|
echo '<button class="woocommerce-widget-layered-nav-dropdown__submit" type="submit" value="' . esc_attr__( 'Apply', 'woocommerce' ) . '">' . esc_html__( 'Apply', 'woocommerce' ) . '</button>';
|
2017-07-10 13:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'or' === $query_type ) {
|
|
|
|
echo '<input type="hidden" name="query_type_' . esc_attr( $taxonomy_filter_name ) . '" value="or" />';
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '<input type="hidden" name="filter_' . esc_attr( $taxonomy_filter_name ) . '" value="' . esc_attr( implode( ',', $current_values ) ) . '" />';
|
2017-11-07 08:55:27 +00:00
|
|
|
echo wc_query_string_form_fields( null, array( 'filter_' . $taxonomy_filter_name, 'query_type_' . $taxonomy_filter_name ), '', true ); // @codingStandardsIgnoreLine
|
2017-07-10 13:36:47 +00:00
|
|
|
echo '</form>';
|
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
wc_enqueue_js( "
|
2017-07-10 13:36:47 +00:00
|
|
|
// Update value on change.
|
2016-09-01 21:33:36 +00:00
|
|
|
jQuery( '.dropdown_layered_nav_" . esc_js( $taxonomy_filter_name ) . "' ).change( function() {
|
2016-02-09 14:41:17 +00:00
|
|
|
var slug = jQuery( this ).val();
|
2017-07-10 13:36:47 +00:00
|
|
|
jQuery( ':input[name=\"filter_" . esc_js( $taxonomy_filter_name ) . "\"]' ).val( slug );
|
2017-07-11 20:21:42 +00:00
|
|
|
|
|
|
|
// Submit form on change if standard dropdown.
|
|
|
|
if ( ! jQuery( this ).attr( 'multiple' ) ) {
|
|
|
|
jQuery( this ).closest( 'form' ).submit();
|
|
|
|
}
|
2016-02-09 14:41:17 +00:00
|
|
|
});
|
2017-02-24 08:18:38 +00:00
|
|
|
|
|
|
|
// Use Select2 enhancement if possible
|
2017-07-10 13:36:47 +00:00
|
|
|
if ( jQuery().selectWoo ) {
|
2017-02-24 08:18:38 +00:00
|
|
|
var wc_layered_nav_select = function() {
|
2017-07-10 13:36:47 +00:00
|
|
|
jQuery( '.dropdown_layered_nav_" . esc_js( $taxonomy_filter_name ) . "' ).selectWoo( {
|
2017-02-24 08:18:38 +00:00
|
|
|
placeholder: '" . esc_html( $any_label ) . "',
|
2017-07-10 13:36:47 +00:00
|
|
|
minimumResultsForSearch: 5,
|
2017-10-09 10:21:21 +00:00
|
|
|
width: '100%',
|
2017-10-09 18:10:32 +00:00
|
|
|
allowClear: " . ( $multiple ? 'false' : 'true' ) . ",
|
|
|
|
language: {
|
|
|
|
noResults: function() {
|
2017-10-09 18:37:09 +00:00
|
|
|
return '" . esc_js( _x( 'No matches found', 'enhanced select', 'woocommerce' ) ) . "';
|
2017-10-09 18:10:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-10 13:36:47 +00:00
|
|
|
} );
|
2017-02-24 08:18:38 +00:00
|
|
|
};
|
|
|
|
wc_layered_nav_select();
|
|
|
|
}
|
2016-02-09 14:41:17 +00:00
|
|
|
" );
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
return $found;
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
/**
|
|
|
|
* Get current page URL for layered nav items.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2017-11-07 08:55:27 +00:00
|
|
|
* @param string $taxonomy Taxonomy.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2016-02-09 14:41:17 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-10 11:16:49 +00:00
|
|
|
protected function get_page_base_url( $taxonomy ) {
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( defined( 'SHOP_IS_ON_FRONT' ) ) {
|
|
|
|
$link = home_url();
|
2016-06-14 10:02:23 +00:00
|
|
|
} elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) {
|
2016-02-09 14:41:17 +00:00
|
|
|
$link = get_post_type_archive_link( 'product' );
|
2016-06-14 10:02:23 +00:00
|
|
|
} elseif ( is_product_category() ) {
|
|
|
|
$link = get_term_link( get_query_var( 'product_cat' ), 'product_cat' );
|
|
|
|
} elseif ( is_product_tag() ) {
|
|
|
|
$link = get_term_link( get_query_var( 'product_tag' ), 'product_tag' );
|
2016-02-09 14:41:17 +00:00
|
|
|
} else {
|
2016-07-12 10:46:16 +00:00
|
|
|
$queried_object = get_queried_object();
|
|
|
|
$link = get_term_link( $queried_object->slug, $queried_object->taxonomy );
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Min/Max.
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( isset( $_GET['min_price'] ) ) {
|
2017-11-07 08:55:27 +00:00
|
|
|
$link = add_query_arg( 'min_price', wc_clean( wp_unslash( $_GET['min_price'] ) ), $link );
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( isset( $_GET['max_price'] ) ) {
|
2017-11-07 08:55:27 +00:00
|
|
|
$link = add_query_arg( 'max_price', wc_clean( wp_unslash( $_GET['max_price'] ) ), $link );
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
2012-10-16 13:06:06 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Order by.
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( isset( $_GET['orderby'] ) ) {
|
2017-11-07 08:55:27 +00:00
|
|
|
$link = add_query_arg( 'orderby', wc_clean( wp_unslash( $_GET['orderby'] ) ), $link );
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-04-27 14:24:10 +00:00
|
|
|
/**
|
|
|
|
* Search Arg.
|
|
|
|
* To support quote characters, first they are decoded from " entities, then URL encoded.
|
|
|
|
*/
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( get_search_query() ) {
|
2017-11-20 12:14:05 +00:00
|
|
|
$link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query( false ) ) ), $link );
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Post Type Arg.
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( isset( $_GET['post_type'] ) ) {
|
2017-11-07 08:55:27 +00:00
|
|
|
$link = add_query_arg( 'post_type', wc_clean( wp_unslash( $_GET['post_type'] ) ), $link );
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Min Rating Arg.
|
2016-12-09 15:43:25 +00:00
|
|
|
if ( isset( $_GET['rating_filter'] ) ) {
|
2017-11-07 08:55:27 +00:00
|
|
|
$link = add_query_arg( 'rating_filter', wc_clean( wp_unslash( $_GET['rating_filter'] ) ), $link );
|
2016-02-09 15:18:27 +00:00
|
|
|
}
|
2012-08-28 15:18:16 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// All current filters.
|
2016-02-10 11:16:49 +00:00
|
|
|
if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) {
|
|
|
|
foreach ( $_chosen_attributes as $name => $data ) {
|
|
|
|
if ( $name === $taxonomy ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$filter_name = sanitize_title( str_replace( 'pa_', '', $name ) );
|
|
|
|
if ( ! empty( $data['terms'] ) ) {
|
|
|
|
$link = add_query_arg( 'filter_' . $filter_name, implode( ',', $data['terms'] ), $link );
|
|
|
|
}
|
|
|
|
if ( 'or' == $data['query_type'] ) {
|
|
|
|
$link = add_query_arg( 'query_type_' . $filter_name, 'or', $link );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
return $link;
|
|
|
|
}
|
2012-08-28 15:18:16 +00:00
|
|
|
|
2016-02-09 21:14:55 +00:00
|
|
|
/**
|
2016-02-10 11:07:42 +00:00
|
|
|
* Count products within certain terms, taking the main WP query into consideration.
|
2016-11-17 22:33:24 +00:00
|
|
|
*
|
2017-06-16 12:35:31 +00:00
|
|
|
* This query allows counts to be generated based on the viewed products, not all products.
|
|
|
|
*
|
2017-11-07 08:55:27 +00:00
|
|
|
* @param array $term_ids Term IDs.
|
|
|
|
* @param string $taxonomy Taxonomy.
|
|
|
|
* @param string $query_type Query Type.
|
2016-02-10 11:07:42 +00:00
|
|
|
* @return array
|
2016-02-09 21:14:55 +00:00
|
|
|
*/
|
2016-02-10 11:07:42 +00:00
|
|
|
protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type ) {
|
2016-02-10 10:02:50 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$tax_query = WC_Query::get_main_tax_query();
|
|
|
|
$meta_query = WC_Query::get_main_meta_query();
|
2016-02-09 21:14:55 +00:00
|
|
|
|
|
|
|
if ( 'or' === $query_type ) {
|
|
|
|
foreach ( $tax_query as $key => $query ) {
|
2016-12-08 10:56:45 +00:00
|
|
|
if ( is_array( $query ) && $taxonomy === $query['taxonomy'] ) {
|
2016-02-09 21:14:55 +00:00
|
|
|
unset( $tax_query[ $key ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 11:36:18 +00:00
|
|
|
$meta_query = new WP_Meta_Query( $meta_query );
|
|
|
|
$tax_query = new WP_Tax_Query( $tax_query );
|
|
|
|
$meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
|
|
|
|
$tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' );
|
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Generate query.
|
2016-06-16 11:36:18 +00:00
|
|
|
$query = array();
|
|
|
|
$query['select'] = "SELECT COUNT( DISTINCT {$wpdb->posts}.ID ) as term_count, terms.term_id as term_count_id";
|
|
|
|
$query['from'] = "FROM {$wpdb->posts}";
|
|
|
|
$query['join'] = "
|
2016-06-15 18:20:48 +00:00
|
|
|
INNER JOIN {$wpdb->term_relationships} AS term_relationships ON {$wpdb->posts}.ID = term_relationships.object_id
|
|
|
|
INNER JOIN {$wpdb->term_taxonomy} AS term_taxonomy USING( term_taxonomy_id )
|
|
|
|
INNER JOIN {$wpdb->terms} AS terms USING( term_id )
|
2016-06-16 11:36:18 +00:00
|
|
|
" . $tax_query_sql['join'] . $meta_query_sql['join'];
|
2016-07-19 13:09:56 +00:00
|
|
|
|
2016-06-16 11:36:18 +00:00
|
|
|
$query['where'] = "
|
|
|
|
WHERE {$wpdb->posts}.post_type IN ( 'product' )
|
2017-11-07 08:55:27 +00:00
|
|
|
AND {$wpdb->posts}.post_status = 'publish'"
|
|
|
|
. $tax_query_sql['where'] . $meta_query_sql['where'] .
|
|
|
|
'AND terms.term_id IN (' . implode( ',', array_map( 'absint', $term_ids ) ) . ')';
|
2016-07-19 13:09:56 +00:00
|
|
|
|
|
|
|
if ( $search = WC_Query::get_main_search_query_sql() ) {
|
|
|
|
$query['where'] .= ' AND ' . $search;
|
2016-07-14 01:44:00 +00:00
|
|
|
}
|
2016-07-19 13:09:56 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
$query['group_by'] = 'GROUP BY terms.term_id';
|
2016-06-16 11:36:18 +00:00
|
|
|
$query = apply_filters( 'woocommerce_get_filtered_term_product_counts_query', $query );
|
|
|
|
$query = implode( ' ', $query );
|
2016-02-09 21:14:55 +00:00
|
|
|
|
2017-06-16 12:35:31 +00:00
|
|
|
// We have a query - let's see if cached results of this query already exist.
|
|
|
|
$query_hash = md5( $query );
|
|
|
|
$cached_counts = (array) get_transient( 'wc_layered_nav_counts' );
|
|
|
|
|
|
|
|
if ( ! isset( $cached_counts[ $query_hash ] ) ) {
|
2017-11-07 08:55:27 +00:00
|
|
|
$results = $wpdb->get_results( $query, ARRAY_A ); // @codingStandardsIgnoreLine
|
2017-06-16 12:35:31 +00:00
|
|
|
$counts = array_map( 'absint', wp_list_pluck( $results, 'term_count', 'term_count_id' ) );
|
|
|
|
$cached_counts[ $query_hash ] = $counts;
|
|
|
|
set_transient( 'wc_layered_nav_counts', $cached_counts, DAY_IN_SECONDS );
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_map( 'absint', (array) $cached_counts[ $query_hash ] );
|
2016-02-09 21:14:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
/**
|
|
|
|
* Show list based layered nav.
|
2016-11-17 22:33:24 +00:00
|
|
|
*
|
2017-11-07 08:55:27 +00:00
|
|
|
* @param array $terms Terms.
|
|
|
|
* @param string $taxonomy Taxonomy.
|
|
|
|
* @param string $query_type Query Type.
|
2016-11-17 22:33:24 +00:00
|
|
|
* @return bool Will nav display?
|
2016-02-09 14:41:17 +00:00
|
|
|
*/
|
|
|
|
protected function layered_nav_list( $terms, $taxonomy, $query_type ) {
|
2017-11-07 08:55:27 +00:00
|
|
|
// List display.
|
2017-07-10 13:36:47 +00:00
|
|
|
echo '<ul class="woocommerce-widget-layered-nav-list">';
|
2014-02-24 15:45:43 +00:00
|
|
|
|
2016-02-10 11:07:42 +00:00
|
|
|
$term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type );
|
2016-02-10 10:19:32 +00:00
|
|
|
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
|
|
|
|
$found = false;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
foreach ( $terms as $term ) {
|
2016-11-17 22:33:24 +00:00
|
|
|
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
|
|
|
|
$option_is_set = in_array( $term->slug, $current_values );
|
|
|
|
$count = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : 0;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Skip the term for the current archive.
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( $this->get_current_term_id() === $term->term_id ) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Only show options with count > 0.
|
2016-02-10 10:02:50 +00:00
|
|
|
if ( 0 < $count ) {
|
|
|
|
$found = true;
|
2016-11-17 22:33:24 +00:00
|
|
|
} elseif ( 0 === $count && ! $option_is_set ) {
|
2016-02-10 10:02:50 +00:00
|
|
|
continue;
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
$filter_name = 'filter_' . sanitize_title( str_replace( 'pa_', '', $taxonomy ) );
|
2017-11-07 08:55:27 +00:00
|
|
|
$current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ $filter_name ] ) ) ) : array();
|
2016-02-09 14:41:17 +00:00
|
|
|
$current_filter = array_map( 'sanitize_title', $current_filter );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( ! in_array( $term->slug, $current_filter ) ) {
|
|
|
|
$current_filter[] = $term->slug;
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-10 11:16:49 +00:00
|
|
|
$link = $this->get_page_base_url( $taxonomy );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
// Add current filters to URL.
|
|
|
|
foreach ( $current_filter as $key => $value ) {
|
2017-11-07 08:55:27 +00:00
|
|
|
// Exclude query arg for current term archive term.
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( $value === $this->get_current_term_slug() ) {
|
|
|
|
unset( $current_filter[ $key ] );
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
// Exclude self so filter can be unset on click.
|
|
|
|
if ( $option_is_set && $value === $term->slug ) {
|
|
|
|
unset( $current_filter[ $key ] );
|
|
|
|
}
|
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( ! empty( $current_filter ) ) {
|
2017-10-27 15:07:30 +00:00
|
|
|
asort( $current_filter );
|
2016-02-09 14:41:17 +00:00
|
|
|
$link = add_query_arg( $filter_name, implode( ',', $current_filter ), $link );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-07 08:55:27 +00:00
|
|
|
// Add Query type Arg to URL.
|
|
|
|
if ( 'or' === $query_type && ! ( 1 === count( $current_filter ) && $option_is_set ) ) {
|
2016-02-09 14:41:17 +00:00
|
|
|
$link = add_query_arg( 'query_type_' . sanitize_title( str_replace( 'pa_', '', $taxonomy ) ), 'or', $link );
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2017-10-27 14:59:47 +00:00
|
|
|
$link = str_replace( '%2C', ',', $link );
|
2016-02-09 14:41:17 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2016-10-11 17:23:25 +00:00
|
|
|
if ( $count > 0 || $option_is_set ) {
|
2017-01-09 19:27:01 +00:00
|
|
|
$link = esc_url( apply_filters( 'woocommerce_layered_nav_link', $link, $term, $taxonomy ) );
|
2017-10-05 17:55:10 +00:00
|
|
|
$term_html = '<a rel="nofollow" href="' . $link . '">' . esc_html( $term->name ) . '</a>';
|
2016-10-11 17:23:25 +00:00
|
|
|
} else {
|
|
|
|
$link = false;
|
|
|
|
$term_html = '<span>' . esc_html( $term->name ) . '</span>';
|
|
|
|
}
|
2016-02-09 14:41:17 +00:00
|
|
|
|
2016-10-11 17:23:25 +00:00
|
|
|
$term_html .= ' ' . apply_filters( 'woocommerce_layered_nav_count', '<span class="count">(' . absint( $count ) . ')</span>', $count, $term );
|
2016-06-08 15:29:27 +00:00
|
|
|
|
2017-07-10 13:36:47 +00:00
|
|
|
echo '<li class="woocommerce-widget-layered-nav-list__item wc-layered-nav-term ' . ( $option_is_set ? 'woocommerce-widget-layered-nav-list__item--chosen chosen' : '' ) . '">';
|
2016-10-11 17:23:25 +00:00
|
|
|
echo wp_kses_post( apply_filters( 'woocommerce_layered_nav_term_html', $term_html, $term, $link, $count ) );
|
2016-06-08 15:29:27 +00:00
|
|
|
echo '</li>';
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2016-02-09 14:41:17 +00:00
|
|
|
|
|
|
|
echo '</ul>';
|
|
|
|
|
|
|
|
return $found;
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|