Guest User

Untitled

a guest
Jul 12th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. function filter_attributes_by_category_shortcode($atts) {
  2. // Shortcode attributes
  3. $atts = shortcode_atts(array(), $atts, 'filter_attributes_by_category');
  4.  
  5. // Get current category
  6. if (is_product_category()) {
  7. $category = get_queried_object();
  8. } else {
  9. return '<p>This filter only works on product category pages.</p>';
  10. }
  11.  
  12. // Get products in the specified category
  13. $args = array(
  14. 'post_type' => 'product',
  15. 'posts_per_page' => -1,
  16. 'tax_query' => array(
  17. array(
  18. 'taxonomy' => 'product_cat',
  19. 'field' => 'slug',
  20. 'terms' => $category->slug,
  21. ),
  22. ),
  23. );
  24.  
  25. $products = new WP_Query($args);
  26. if (!$products->have_posts()) {
  27. return '<p>No products found in this category.</p>';
  28. }
  29.  
  30. $attributes = array();
  31.  
  32. // Loop through the products to get their attributes
  33. while ($products->have_posts()) : $products->the_post();
  34. $product = wc_get_product(get_the_ID());
  35. $product_attributes = $product->get_attributes();
  36.  
  37. foreach ($product_attributes as $attribute) {
  38. if ($attribute->is_taxonomy()) {
  39. $taxonomy = $attribute->get_taxonomy_object();
  40. $terms = wp_get_post_terms($product->get_id(), $attribute->get_name(), 'all');
  41.  
  42. foreach ($terms as $term) {
  43. if (!isset($attributes[$taxonomy->attribute_label])) {
  44. $attributes[$taxonomy->attribute_label] = array();
  45. }
  46. $attributes[$taxonomy->attribute_label][$term->term_id] = $term->name;
  47. }
  48. }
  49. }
  50. endwhile;
  51. wp_reset_postdata();
  52.  
  53. // Generate the filter form
  54. $output = '<form method="GET" action="' . esc_url(home_url('/f')) . '">';
  55. foreach ($attributes as $attribute_label => $terms) {
  56. if (!empty($terms)) {
  57. $output .= '<label for="' . esc_attr(sanitize_title($attribute_label)) . '">' . esc_html($attribute_label) . ':</label>';
  58. $output .= '<select name="' . esc_attr(sanitize_title($attribute_label)) . '" id="' . esc_attr(sanitize_title($attribute_label)) . '">';
  59. $output .= '<option value="">' . __('Select', 'woocommerce') . '</option>';
  60.  
  61. foreach ($terms as $term_id => $term_name) {
  62. $selected = isset($_GET[sanitize_title($attribute_label)]) && $_GET[sanitize_title($attribute_label)] == $term_name ? ' selected' : '';
  63. $output .= '<option value="' . esc_attr($term_name) . '"' . $selected . '>' . esc_html($term_name) . '</option>';
  64. }
  65.  
  66. $output .= '</select><br>';
  67. }
  68. }
  69.  
  70. $output .= '<button type="submit">' . __('Filter', 'woocommerce') . '</button>';
  71. $output .= '</form>';
  72.  
  73. return $output;
  74. }
  75. add_shortcode('filter_attributes_by_category', 'filter_attributes_by_category_shortcode');
  76.  
  77. function filter_products_by_attributes($query) {
  78. if (!is_admin() && $query->is_main_query() && is_tax('product_cat')) {
  79. $tax_query = array();
  80.  
  81. foreach (wc_get_attribute_taxonomies() as $tax) {
  82. $taxonomy = wc_attribute_taxonomy_name($tax->attribute_name);
  83.  
  84. if (isset($_GET[sanitize_title($tax->attribute_label)]) && !empty($_GET[sanitize_title($tax->attribute_label)])) {
  85. $term = get_term_by('name', sanitize_text_field($_GET[sanitize_title($tax->attribute_label)]), $taxonomy);
  86. if ($term) {
  87. $tax_query[] = array(
  88. 'taxonomy' => $taxonomy,
  89. 'field' => 'slug',
  90. 'terms' => $term->slug,
  91. );
  92. }
  93. }
  94. }
  95.  
  96. if (!empty($tax_query)) {
  97. $query->set('tax_query', $tax_query);
  98. }
  99. }
  100. }
  101. add_action('pre_get_posts', 'filter_products_by_attributes');
  102.  
Advertisement
Add Comment
Please, Sign In to add comment