408 lines
12 KiB
PHP
408 lines
12 KiB
PHP
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Layered Navigation Widget.
|
|
*
|
|
* @author WooThemes
|
|
* @category Widgets
|
|
* @package WooCommerce/Widgets
|
|
* @version 2.6.0
|
|
* @extends WC_Widget
|
|
*/
|
|
class WC_Widget_Layered_Nav extends WC_Widget {
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public function __construct() {
|
|
$this->widget_cssclass = 'woocommerce widget_layered_nav';
|
|
$this->widget_description = __( 'Shows a custom attribute in a widget which lets you narrow down the list of products when viewing product categories.', 'woocommerce' );
|
|
$this->widget_id = 'woocommerce_layered_nav';
|
|
$this->widget_name = __( 'WooCommerce Layered Nav', 'woocommerce' );
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Updates a particular instance of a widget.
|
|
*
|
|
* @see WP_Widget->update
|
|
*
|
|
* @param array $new_instance
|
|
* @param array $old_instance
|
|
*
|
|
* @return array
|
|
*/
|
|
public function update( $new_instance, $old_instance ) {
|
|
$this->init_settings();
|
|
return parent::update( $new_instance, $old_instance );
|
|
}
|
|
|
|
/**
|
|
* Outputs the settings update form.
|
|
*
|
|
* @see WP_Widget->form
|
|
*
|
|
* @param array $instance
|
|
*/
|
|
public function form( $instance ) {
|
|
$this->init_settings();
|
|
parent::form( $instance );
|
|
}
|
|
|
|
/**
|
|
* Init settings after post types are registered.
|
|
*/
|
|
public function init_settings() {
|
|
$attribute_array = array();
|
|
$attribute_taxonomies = wc_get_attribute_taxonomies();
|
|
|
|
if ( $attribute_taxonomies ) {
|
|
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(
|
|
'type' => 'text',
|
|
'std' => __( 'Filter by', 'woocommerce' ),
|
|
'label' => __( 'Title', 'woocommerce' )
|
|
),
|
|
'attribute' => array(
|
|
'type' => 'select',
|
|
'std' => '',
|
|
'label' => __( 'Attribute', 'woocommerce' ),
|
|
'options' => $attribute_array
|
|
),
|
|
'display_type' => array(
|
|
'type' => 'select',
|
|
'std' => 'list',
|
|
'label' => __( 'Display type', 'woocommerce' ),
|
|
'options' => array(
|
|
'list' => __( 'List', 'woocommerce' ),
|
|
'dropdown' => __( 'Dropdown', 'woocommerce' )
|
|
)
|
|
),
|
|
'query_type' => array(
|
|
'type' => 'select',
|
|
'std' => 'and',
|
|
'label' => __( 'Query type', 'woocommerce' ),
|
|
'options' => array(
|
|
'and' => __( 'AND', 'woocommerce' ),
|
|
'or' => __( 'OR', 'woocommerce' )
|
|
)
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Output widget.
|
|
*
|
|
* @see WP_Widget
|
|
*
|
|
* @param array $args
|
|
* @param array $instance
|
|
*/
|
|
public function widget( $args, $instance ) {
|
|
global $_chosen_attributes;
|
|
|
|
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
|
|
return;
|
|
}
|
|
|
|
$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'];
|
|
|
|
if ( ! taxonomy_exists( $taxonomy ) ) {
|
|
return;
|
|
}
|
|
|
|
$get_terms_args = array( 'hide_empty' => '1' );
|
|
|
|
$orderby = wc_attribute_orderby( $taxonomy );
|
|
|
|
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 );
|
|
|
|
if ( 0 === sizeof( $terms ) ) {
|
|
return;
|
|
}
|
|
|
|
ob_start();
|
|
|
|
$this->widget_start( $args, $instance );
|
|
|
|
if ( 'dropdown' === $display_type ) {
|
|
$found = $this->layered_nav_dropdown( $terms, $taxonomy, $query_type );
|
|
} else {
|
|
$found = $this->layered_nav_list( $terms, $taxonomy, $query_type );
|
|
}
|
|
|
|
$this->widget_end( $args );
|
|
|
|
// Force found when option is selected - do not force found on taxonomy attributes
|
|
if ( ! is_tax() && is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) {
|
|
$found = true;
|
|
}
|
|
|
|
if ( ! $found ) {
|
|
ob_end_clean();
|
|
} else {
|
|
echo ob_get_clean();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return the currently viewed taxonomy name.
|
|
* @return string
|
|
*/
|
|
protected function get_current_taxonomy() {
|
|
return is_tax() ? get_queried_object()->taxonomy : '';
|
|
}
|
|
|
|
/**
|
|
* Return the currently viewed term ID.
|
|
* @return int
|
|
*/
|
|
protected function get_current_term_id() {
|
|
return absint( is_tax() ? get_queried_object()->term_id : 0 );
|
|
}
|
|
|
|
/**
|
|
* Return the currently viewed term slug.
|
|
* @return int
|
|
*/
|
|
protected function get_current_term_slug() {
|
|
return absint( is_tax() ? get_queried_object()->slug : 0 );
|
|
}
|
|
|
|
/**
|
|
* Show dropdown layered nav.
|
|
* @param array $terms
|
|
* @param string $taxonomy
|
|
* @param string $query_type
|
|
* @return bool Will nav display?
|
|
*/
|
|
protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) {
|
|
global $_chosen_attributes;
|
|
|
|
$found = false;
|
|
|
|
if ( $taxonomy !== $this->get_current_taxonomy() ) {
|
|
$taxonomy_filter_name = str_replace( 'pa_', '', $taxonomy );
|
|
|
|
echo '<select class="dropdown_layered_nav_' . esc_attr( $taxonomy_filter_name ) . '">';
|
|
echo '<option value="">' . sprintf( __( 'Any %s', 'woocommerce' ), wc_attribute_label( $taxonomy ) ) . '</option>';
|
|
|
|
foreach ( $terms as $term ) {
|
|
|
|
// If on a term page, skip that term in widget list
|
|
if ( $term->term_id === $this->get_current_term_id() ) {
|
|
continue;
|
|
}
|
|
|
|
// Get count based on current view
|
|
$_products_in_term = wc_get_term_product_ids( $term->term_id, $taxonomy );
|
|
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
|
|
$option_is_set = in_array( $term->slug, $current_values );
|
|
|
|
// If this is an AND query, only show options with count > 0
|
|
if ( 'and' === $query_type ) {
|
|
$count = sizeof( array_intersect( $_products_in_term, WC()->query->filtered_product_ids ) );
|
|
|
|
if ( 0 < $count ) {
|
|
$found = true;
|
|
}
|
|
|
|
if ( 0 === $count && ! $option_is_set ) {
|
|
continue;
|
|
}
|
|
|
|
// If this is an OR query, show all options so search can be expanded
|
|
} else {
|
|
$count = sizeof( array_intersect( $_products_in_term, WC()->query->unfiltered_product_ids ) );
|
|
|
|
if ( 0 < $count ) {
|
|
$found = true;
|
|
}
|
|
}
|
|
|
|
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( $option_is_set, true, false ) . '>' . esc_html( $term->name ) . '</option>';
|
|
}
|
|
|
|
echo '</select>';
|
|
|
|
wc_enqueue_js( "
|
|
jQuery( '.dropdown_layered_nav_". esc_js( $taxonomy_filter_name ) . "' ).change( function() {
|
|
var slug = jQuery( this ).val();
|
|
location.href = '" . preg_replace( '%\/page\/[0-9]+%', '', str_replace( array( '&', '%2C' ), array( '&', ',' ), esc_js( add_query_arg( 'filtering', '1', remove_query_arg( array( 'page', 'filter_' . $taxonomy_filter_name ) ) ) ) ) ) . "&filter_". esc_js( $taxonomy_filter_name ) . "=' + slug;
|
|
});
|
|
" );
|
|
}
|
|
|
|
return $found;
|
|
}
|
|
|
|
/**
|
|
* Get current page URL for layered nav items.
|
|
* @return string
|
|
*/
|
|
protected function get_page_base_url() {
|
|
if ( defined( 'SHOP_IS_ON_FRONT' ) ) {
|
|
$link = home_url();
|
|
} elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id('shop') ) ) {
|
|
$link = get_post_type_archive_link( 'product' );
|
|
} else {
|
|
$link = get_term_link( get_query_var('term'), get_query_var('taxonomy') );
|
|
}
|
|
|
|
// Min/Max
|
|
if ( isset( $_GET['min_price'] ) ) {
|
|
$link = add_query_arg( 'min_price', wc_clean( $_GET['min_price'] ), $link );
|
|
}
|
|
|
|
if ( isset( $_GET['max_price'] ) ) {
|
|
$link = add_query_arg( 'max_price', wc_clean( $_GET['max_price'] ), $link );
|
|
}
|
|
|
|
// Orderby
|
|
if ( isset( $_GET['orderby'] ) ) {
|
|
$link = add_query_arg( 'orderby', wc_clean( $_GET['orderby'] ), $link );
|
|
}
|
|
|
|
// Search Arg
|
|
if ( get_search_query() ) {
|
|
$link = add_query_arg( 's', get_search_query(), $link );
|
|
}
|
|
|
|
// Post Type Arg
|
|
if ( isset( $_GET['post_type'] ) ) {
|
|
$link = add_query_arg( 'post_type', wc_clean( $_GET['post_type'] ), $link );
|
|
}
|
|
|
|
return $link;
|
|
}
|
|
|
|
/**
|
|
* Show list based layered nav.
|
|
* @param array $terms
|
|
* @param string $taxonomy
|
|
* @param string $query_type
|
|
* @return bool Will nav display?
|
|
*/
|
|
protected function layered_nav_list( $terms, $taxonomy, $query_type ) {
|
|
global $_chosen_attributes;
|
|
|
|
// List display
|
|
echo '<ul>';
|
|
|
|
// flip the filtered_products_ids array so that we can use the more efficient array_intersect_key
|
|
$filtered_product_ids = array_flip( WC()->query->filtered_product_ids );
|
|
$unfiltered_product_ids = array_flip( WC()->query->unfiltered_product_ids );
|
|
$found = false;
|
|
|
|
foreach ( $terms as $term ) {
|
|
// Get count based on current view - uses transients
|
|
// flip the product_in_term array so that we can use array_intersect_key
|
|
$_products_in_term = array_flip( wc_get_term_product_ids( $term->term_id, $taxonomy ) );
|
|
$current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array();
|
|
$option_is_set = in_array( $term->slug, $current_values );
|
|
|
|
// skip the term for the current archive
|
|
if ( $this->get_current_term_id() === $term->term_id ) {
|
|
continue;
|
|
}
|
|
|
|
// If this is an AND query, only show options with count > 0
|
|
if ( 'and' === $query_type ) {
|
|
// Intersect both arrays now they have been flipped so that we can use their keys
|
|
$count = sizeof( array_intersect_key( $_products_in_term, $filtered_product_ids ) );
|
|
|
|
if ( 0 < $count ) {
|
|
$found = true;
|
|
}
|
|
|
|
if ( 0 === $count && ! $option_is_set ) {
|
|
continue;
|
|
}
|
|
|
|
// If this is an OR query, show all options so search can be expanded
|
|
} else {
|
|
// Intersect both arrays now they have been flipped so that we can use their keys
|
|
$count = sizeof( array_intersect_key( $_products_in_term, $unfiltered_product_ids ) );
|
|
|
|
if ( 0 < $count ) {
|
|
$found = true;
|
|
}
|
|
}
|
|
|
|
$filter_name = 'filter_' . sanitize_title( str_replace( 'pa_', '', $taxonomy ) );
|
|
$current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( $_GET[ $filter_name ] ) ) : array();
|
|
$current_filter = array_map( 'sanitize_title', $current_filter );
|
|
|
|
if ( ! in_array( $term->slug, $current_filter ) ) {
|
|
$current_filter[] = $term->slug;
|
|
}
|
|
|
|
$link = $this->get_page_base_url();
|
|
|
|
// Add current filters to URL.
|
|
foreach ( $current_filter as $key => $value ) {
|
|
// Exclude query arg for current term archive term
|
|
if ( $value === $this->get_current_term_slug() ) {
|
|
unset( $current_filter[ $key ] );
|
|
}
|
|
|
|
// Exclude self so filter can be unset on click.
|
|
if ( $option_is_set && $value === $term->slug ) {
|
|
unset( $current_filter[ $key ] );
|
|
}
|
|
}
|
|
|
|
if ( ! empty( $current_filter ) ) {
|
|
$link = add_query_arg( $filter_name, implode( ',', $current_filter ), $link );
|
|
|
|
// Add Query type Arg to URL
|
|
if ( $query_type === 'or' && ! ( 1 === sizeof( $current_filter ) && $option_is_set ) ) {
|
|
$link = add_query_arg( 'query_type_' . sanitize_title( str_replace( 'pa_', '', $taxonomy ) ), 'or', $link );
|
|
}
|
|
}
|
|
|
|
echo '<li class="wc-layered-nav-term ' . ( $option_is_set ? 'chosen' : '' ) . '">';
|
|
|
|
echo ( $count > 0 || $option_is_set ) ? '<a href="' . esc_url( apply_filters( 'woocommerce_layered_nav_link', $link ) ) . '">' : '<span>';
|
|
|
|
echo esc_html( $term->name );
|
|
|
|
echo ( $count > 0 || $option_is_set ) ? '</a>' : '</span>';
|
|
|
|
echo ' <span class="count">(' . absint( $count ) . ')</span></li>';
|
|
}
|
|
|
|
echo '</ul>';
|
|
|
|
return $found;
|
|
}
|
|
}
|