Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function filter_attributes_by_category_shortcode($atts) {
- // Shortcode attributes
- $atts = shortcode_atts(array(), $atts, 'filter_attributes_by_category');
- // Get current category
- if (is_product_category()) {
- $category = get_queried_object();
- } else {
- return '<p>This filter only works on product category pages.</p>';
- }
- // Get products in the specified category
- $args = array(
- 'post_type' => 'product',
- 'posts_per_page' => -1,
- 'tax_query' => array(
- array(
- 'taxonomy' => 'product_cat',
- 'field' => 'slug',
- 'terms' => $category->slug,
- ),
- ),
- );
- $products = new WP_Query($args);
- if (!$products->have_posts()) {
- return '<p>No products found in this category.</p>';
- }
- $attributes = array();
- // Loop through the products to get their attributes
- while ($products->have_posts()) : $products->the_post();
- $product = wc_get_product(get_the_ID());
- $product_attributes = $product->get_attributes();
- foreach ($product_attributes as $attribute) {
- if ($attribute->is_taxonomy()) {
- $taxonomy = $attribute->get_taxonomy_object();
- $terms = wp_get_post_terms($product->get_id(), $attribute->get_name(), 'all');
- foreach ($terms as $term) {
- if (!isset($attributes[$taxonomy->attribute_label])) {
- $attributes[$taxonomy->attribute_label] = array();
- }
- $attributes[$taxonomy->attribute_label][$term->term_id] = $term->name;
- }
- }
- }
- endwhile;
- wp_reset_postdata();
- // Generate the filter form
- $output = '<form method="GET" action="' . esc_url(home_url('/f')) . '">';
- foreach ($attributes as $attribute_label => $terms) {
- if (!empty($terms)) {
- $output .= '<label for="' . esc_attr(sanitize_title($attribute_label)) . '">' . esc_html($attribute_label) . ':</label>';
- $output .= '<select name="' . esc_attr(sanitize_title($attribute_label)) . '" id="' . esc_attr(sanitize_title($attribute_label)) . '">';
- $output .= '<option value="">' . __('Select', 'woocommerce') . '</option>';
- foreach ($terms as $term_id => $term_name) {
- $selected = isset($_GET[sanitize_title($attribute_label)]) && $_GET[sanitize_title($attribute_label)] == $term_name ? ' selected' : '';
- $output .= '<option value="' . esc_attr($term_name) . '"' . $selected . '>' . esc_html($term_name) . '</option>';
- }
- $output .= '</select><br>';
- }
- }
- $output .= '<button type="submit">' . __('Filter', 'woocommerce') . '</button>';
- $output .= '</form>';
- return $output;
- }
- add_shortcode('filter_attributes_by_category', 'filter_attributes_by_category_shortcode');
- function filter_products_by_attributes($query) {
- if (!is_admin() && $query->is_main_query() && is_tax('product_cat')) {
- $tax_query = array();
- foreach (wc_get_attribute_taxonomies() as $tax) {
- $taxonomy = wc_attribute_taxonomy_name($tax->attribute_name);
- if (isset($_GET[sanitize_title($tax->attribute_label)]) && !empty($_GET[sanitize_title($tax->attribute_label)])) {
- $term = get_term_by('name', sanitize_text_field($_GET[sanitize_title($tax->attribute_label)]), $taxonomy);
- if ($term) {
- $tax_query[] = array(
- 'taxonomy' => $taxonomy,
- 'field' => 'slug',
- 'terms' => $term->slug,
- );
- }
- }
- }
- if (!empty($tax_query)) {
- $query->set('tax_query', $tax_query);
- }
- }
- }
- add_action('pre_get_posts', 'filter_products_by_attributes');
Advertisement
Add Comment
Please, Sign In to add comment