esc_html__( 'WOODMART WooCommerce Layered Nav', 'woodmart' ), // Widget Backend Description 'description' => esc_html__( 'Shows a custom attribute in a widget which lets you narrow down the list of products when viewing product categories.', 'woodmart' ), 'slug' => 'woodmart-woocommerce-layered-nav', ); // Configure the widget fields // fields array $args['fields'] = array( array( 'id' => 'title', 'type' => 'text', 'std' => esc_html__( 'Filter by', 'woodmart' ), 'name' => esc_html__( 'Title', 'woodmart' ), ), array( 'id' => 'attribute', 'type' => 'dropdown', 'std' => '', 'name' => esc_html__( 'Attribute', 'woodmart' ), 'callback' => 'get_layered_get_attributes_options', ), array( 'id' => 'category', 'type' => 'select2', 'default' => array( 'all' ), 'name' => esc_html__( 'Show on category', 'woodmart' ), 'callback' => 'get_layered_get_categories_options', 'fields' => array(), ), array( 'id' => 'query_type', 'type' => 'dropdown', 'std' => 'and', 'name' => esc_html__( 'Query type', 'woodmart' ), 'fields' => array( esc_html__( 'AND', 'woodmart' ) => 'and', esc_html__( 'OR', 'woodmart' ) => 'or', ), ), array( 'id' => 'display', 'param_name' => 'display', 'type' => 'dropdown', 'std' => 'list', 'name' => esc_html__( 'Layout', 'woodmart' ), 'fields' => array( esc_html__( 'List', 'woodmart' ) => 'list', esc_html__( '2 columns', 'woodmart' ) => 'double', esc_html__( 'Inline', 'woodmart' ) => 'inline', esc_html__( 'Dropdown', 'woodmart' ) => 'dropdown' ) ), array( 'id' => 'size', 'type' => 'dropdown', 'std' => 'normal', 'name' => esc_html__( 'Swatches size', 'woodmart' ), 'fields' => array( esc_html__( 'Small', 'woodmart' ) => 'small', esc_html__( 'Medium', 'woodmart' ) => 'normal', esc_html__( 'Large', 'woodmart' ) => 'large', ) ), array( 'id' => 'style', 'type' => 'dropdown', 'std' => 'inherit', 'name' => esc_html__( 'Swatch style', 'woodmart' ), 'fields' => array( esc_html__( 'Inherit', 'woodmart' ) => 'inherit', esc_html__( 'Style 1', 'woodmart' ) => '1', esc_html__( 'Style 2', 'woodmart' ) => '2', esc_html__( 'Style 3', 'woodmart' ) => '3', esc_html__( 'Style 4', 'woodmart' ) => '4', ), ), array( 'id' => 'shape', 'type' => 'dropdown', 'std' => 'round', 'name' => esc_html__( 'Swatches shape', 'woodmart' ), 'fields' => array( esc_html__( 'Inherit', 'woodmart' ) => 'inherit', esc_html__( 'Round', 'woodmart' ) => 'round', esc_html__( 'Rounded', 'woodmart' ) => 'rounded', esc_html__( 'Square', 'woodmart' ) => 'square', ), ), array( 'id' => 'labels', 'type' => 'dropdown', 'std' => 'on', 'name' => esc_html__( 'Show labels', 'woodmart' ), 'fields' => array( esc_html__( 'ON', 'woodmart' ) => 'on', esc_html__( 'OFF', 'woodmart' ) => 'off', ), ), array( 'id' => 'tooltips', 'type' => 'dropdown', 'std' => 'on', 'name' => esc_html__( 'Show tooltips', 'woodmart' ), 'fields' => array( esc_html__( 'OFF', 'woodmart' ) => 'off', esc_html__( 'ON', 'woodmart' ) => 'on', ), ), array( 'id' => 'search_by_filters', 'type' => 'checkbox', 'name' => esc_html__( 'Show search input for this attribute', 'woodmart' ), 'dependency' => array( 'element' => 'display', 'value_not_equal_to' => array( 'dropdown' ), ), ), ); // create widget $this->create_widget( $args ); } // Output function // Based on woo widget @version 2.3.0 function widget( $args, $instance ) { $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes(); $taxonomy = isset( $instance['attribute'] ) ? wc_attribute_taxonomy_name( $instance['attribute'] ) : ''; $category = isset( $instance['category'] ) ? $instance['category'] : array( 'all' ); $query_type = isset( $instance['query_type'] ) ? $instance['query_type'] : 'and'; $display = isset( $instance['display'] ) ? $instance['display'] : 'list'; $template = isset( $instance['template'] ) ? $instance['template'] : 'default'; $wrapper_classes = ''; if ( ! is_shop() && ! is_product_taxonomy() && $template == 'default' ) { return; } $current_cat = get_queried_object(); if ( ! is_array( $category ) ) { $category = explode( ',', $category ); } if ( ! is_tax() && ! in_array( 'all', $category ) ) { return; } if ( ! in_array( 'all', $category ) && property_exists( $current_cat, 'term_id' ) && ! in_array( $current_cat->term_id, $category ) && ! in_array( $current_cat->parent, $category ) ) { return; } 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; } if ( 'layered-nav' === woodmart_get_opt( 'shop_widgets_collapse' ) ) { $wrapper_classes .= ' wd-widget-collapse'; } if ( 'disable' !== woodmart_get_opt( 'shop_widgets_collapse', 'disable' ) && is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) { $wrapper_classes .= ' wd-opened-initially wd-opened'; } if ( $wrapper_classes ) { $args['before_widget'] = str_replace( 'class="', 'class="' . $wrapper_classes . ' ', $args['before_widget'] ); } if ( isset( $instance['search_by_filters'] ) && $instance['search_by_filters'] ) { woodmart_enqueue_inline_style( 'filter-search' ); woodmart_enqueue_js_script( 'search-by-filters' ); } ob_start(); echo wp_kses_post( $args['before_widget'] ); if ( $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance ) ) { echo wp_kses_post( $args['before_title'] ) . $title . wp_kses_post( $args['after_title'] ); } if ( $template == 'default' ) { if ( 'dropdown' === $display ) { wp_enqueue_script( 'selectWoo' ); wp_enqueue_style( 'select2' ); $found = $this->layered_nav_dropdown( $terms, $taxonomy, $query_type ); } else { $found = $this->layered_nav_list( $terms, $taxonomy, $query_type, $instance ); } } else { $found = $this->layered_nav_checkbox_list( $terms, $taxonomy, $query_type, $instance ); } echo wp_kses_post( $args['after_widget'] ); // 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 Terms. * @param string $taxonomy Taxonomy. * @param string $query_type Query Type. * @return bool Will nav display? */ protected function layered_nav_dropdown( $terms, $taxonomy, $query_type ) { global $wp; $found = false; if ( $taxonomy !== $this->get_current_taxonomy() ) { $term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type ); $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes(); $taxonomy_filter_name = str_replace( 'pa_', '', $taxonomy ); $taxonomy_label = wc_attribute_label( $taxonomy ); /* translators: %s: taxonomy name */ $any_label = apply_filters( 'woocommerce_layered_nav_any_label', sprintf( __( 'Any %s', 'woodmart' ), $taxonomy_label ), $taxonomy_label, $taxonomy ); $multiple = 'or' === $query_type; $current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array(); if ( '' === get_option( 'permalink_structure' ) ) { $form_action = remove_query_arg( array( 'page', 'paged' ), add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) ); } else { $form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( trailingslashit( $wp->request ) ) ); } woodmart_enqueue_js_script( 'filter-dropdowns' ); echo '
'; } return $found; } /** * Get current page URL for layered nav items. * * @return string */ protected function get_page_base_url() { if ( Automattic\Jetpack\Constants::is_defined( 'SHOP_IS_ON_FRONT' ) ) { $link = home_url(); } elseif ( is_shop() ) { $link = get_permalink( wc_get_page_id( 'shop' ) ); } 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' ); } else { $queried_object = get_queried_object(); $link = get_term_link( $queried_object->slug, $queried_object->taxonomy ); } // Min/Max. if ( isset( $_GET['min_price'] ) ) { $link = add_query_arg( 'min_price', wc_clean( wp_unslash( $_GET['min_price'] ) ), $link ); } if ( isset( $_GET['max_price'] ) ) { $link = add_query_arg( 'max_price', wc_clean( wp_unslash( $_GET['max_price'] ) ), $link ); } // Order by. if ( isset( $_GET['orderby'] ) ) { $link = add_query_arg( 'orderby', wc_clean( wp_unslash( $_GET['orderby'] ) ), $link ); } /** * Search Arg. * To support quote characters, first they are decoded from " entities, then URL encoded. */ if ( get_search_query() ) { $link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query() ) ), $link ); } // Post Type Arg if ( isset( $_GET['post_type'] ) ) { $link = add_query_arg( 'post_type', wc_clean( wp_unslash( $_GET['post_type'] ) ), $link ); // Prevent post type and page id when pretty permalinks are disabled. if ( is_shop() ) { $link = remove_query_arg( 'page_id', $link ); } } // Min Rating Arg if ( isset( $_GET['rating_filter'] ) ) { $link = add_query_arg( 'rating_filter', wc_clean( wp_unslash( $_GET['rating_filter'] ) ), $link ); } // All current filters. if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) { // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found, WordPress.CodeAnalysis.AssignmentInCondition.Found foreach ( $_chosen_attributes as $name => $data ) { $filter_name = wc_attribute_taxonomy_slug( $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 ); } } } return apply_filters( 'woocommerce_widget_get_current_page_url', $link, $this ); } /** * Count products within certain terms, taking the main WP query into consideration. * * This query allows counts to be generated based on the viewed products, not all products. * * @param array $term_ids Term IDs. * @param string $taxonomy Taxonomy. * @param string $query_type Query Type. * @return array */ protected function get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type ) { if ( function_exists( 'WC' ) && version_compare( WC()->version, '5.5.0', '<' ) ) { global $wpdb; $tax_query = WC_Query::get_main_tax_query(); $meta_query = WC_Query::get_main_meta_query(); if ( 'or' === $query_type ) { foreach ( $tax_query as $key => $query ) { if ( is_array( $query ) && $taxonomy === $query['taxonomy'] ) { unset( $tax_query[ $key ] ); } } } $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' ); // Generate query $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'] = " 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 ) " . $tax_query_sql['join'] . $meta_query_sql['join']; $query['where'] = " WHERE {$wpdb->posts}.post_type IN ( 'product' ) 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 ) ) . ') '; if ( $search = WC_Query::get_main_search_query_sql() ) { $query['where'] .= ' AND ' . $search; if ( woodmart_get_opt( 'search_by_sku' ) ) { // search for variations with a matching sku and return the parent. $sku_to_parent_id = $wpdb->get_col( $wpdb->prepare( "SELECT p.post_parent as post_id FROM {$wpdb->posts} as p join {$wpdb->wc_product_meta_lookup} ml on p.ID = ml.product_id and ml.sku LIKE '%%%s%%' where p.post_parent <> 0 group by p.post_parent", wc_clean( $_GET['s'] ) ) ); // Search for a regular product that matches the sku. $sku_to_id = $wpdb->get_col( $wpdb->prepare( "SELECT product_id FROM {$wpdb->wc_product_meta_lookup} WHERE sku LIKE '%%%s%%';", wc_clean( $_GET['s'] ) ) ); $search_ids = array_merge( $sku_to_id, $sku_to_parent_id ); $search_ids = array_filter( array_map( 'absint', $search_ids ) ); if ( sizeof( $search_ids ) > 0 ) { $query['where'] = str_replace( '))', ") OR ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ')))', $query['where'] ); } } } $query['group_by'] = 'GROUP BY terms.term_id'; $query = apply_filters( 'woocommerce_get_filtered_term_product_counts_query', $query ); $query = implode( ' ', $query ); // We have a query - let's see if cached results of this query already exist. $query_hash = md5( $query ); // Maybe store a transient of the count values. $cache = apply_filters( 'woocommerce_layered_nav_count_maybe_cache', true ); if ( true === $cache ) { $cached_counts = (array) get_transient( 'wc_layered_nav_counts_' . sanitize_title( $taxonomy ) ); } else { $cached_counts = array(); } if ( ! isset( $cached_counts[ $query_hash ] ) ) { $results = $wpdb->get_results( $query, ARRAY_A ); // @codingStandardsIgnoreLine $counts = array_map( 'absint', wp_list_pluck( $results, 'term_count', 'term_count_id' ) ); $cached_counts[ $query_hash ] = $counts; if ( true === $cache ) { set_transient( 'wc_layered_nav_counts_' . sanitize_title( $taxonomy ), $cached_counts, DAY_IN_SECONDS ); } } return array_map( 'absint', (array) $cached_counts[ $query_hash ] ); } else { return wc_get_container()->get( Filterer::class )->get_filtered_term_product_counts( $term_ids, $taxonomy, $query_type ); } } /** * 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, $instance ) { $labels = isset( $instance['labels'] ) ? $instance['labels'] : 'on'; $tooltips = isset( $instance['tooltips'] ) ? $instance['tooltips'] : 'off'; $size = isset( $instance['size'] ) ? $instance['size'] : 'normal'; $style = isset( $instance['style'] ) ? $instance['style'] : 'inherit'; $shape = isset( $instance['shape'] ) ? $instance['shape'] : 'round'; $display = isset( $instance['display'] ) ? $instance['display'] : 'list'; $search_by_filters = isset( $instance['search_by_filters'] ) ? $instance['search_by_filters'] : 0; $scroll_for_widget = woodmart_get_opt( 'widgets_scroll' ); $is_brand = ( woodmart_get_opt( 'brands_attribute' ) == $taxonomy ); if ( 'inherit' === $style ) { $style = woodmart_wc_get_attribute_term( $taxonomy, 'swatch_style' ); } if ( 'inherit' === $shape ) { $shape = woodmart_wc_get_attribute_term( $taxonomy, 'swatch_shape' ); if ( ! $shape ) { $shape = 'round'; } } $class = 'wd-labels-' . $labels; $class .= ' wd-size-' . $size; $class .= ' wd-layout-' . $display; if ( $style ) { $class .= ' wd-text-style-' . $style; woodmart_enqueue_inline_style( 'woo-mod-swatches-style-' . $style ); } else { $class .= ' wd-text-style-1'; woodmart_enqueue_inline_style( 'woo-mod-swatches-style-1' ); } if ( $is_brand ) { $class .= ' wd-swatches-brands'; } else { if ( $style ) { $class .= ' wd-bg-style-' . $style; } else { $class .= ' wd-bg-style-4'; woodmart_enqueue_inline_style( 'woo-mod-swatches-style-4' ); } $class .= ' wd-shape-' . $shape; } if ( in_array( $size, array( 'small', 'normal', 'large' ), true ) ) { $class .= woodmart_get_old_classes( ' swatches-' . $size ); } if ( $search_by_filters ) { $taxonomy_object = get_taxonomy( $taxonomy ); if ( isset( $taxonomy_object->labels->singular_name ) ) { $label = $taxonomy_object->labels->singular_name; } else { $label = str_replace( 'pa_', ' ', $taxonomy ); } $placeholder = sprintf( esc_html__( 'Find a %s', 'woodmart' ), $label ); ?>