I am trying to make a filter in WordPress WooCommerse. The filter for products is located on the page where the products has been shown by shortcode. The code I have already made can’t filter the products by color, just show string “[Object object]” instead.
This is the code i tried to create filter:
`<?php
// Function to generate a filter form
function get_custom_attribute_filter_form( $attribute ) {
$output = '<div id="custom-attribute-filter">';
$taxonomy = 'pa_' . $attribute;
if ( taxonomy_exists( $taxonomy ) ) {
$terms = get_terms( array(
'taxonomy' => $taxonomy,
'hide_empty' => false, // Set to false to show all terms
) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$output .= '<label for="filter-' . esc_attr( $attribute ) . '">' . ucfirst( $attribute ) . ':</label>';
$output .= '<select id="filter-' . esc_attr( $attribute ) . '" name="' . esc_attr( $attribute ) . '">';
$output .= '<option value="">' . __( 'All', 'text_domain' ) . '</option>';
foreach ( $terms as $term ) {
$output .= '<option value="' . esc_attr( $term->slug ) . '">' . esc_html( $term->name ) . '</option>';
}
$output .= '</select>';
}
}
$output .= '</div>';
$output .= '<div id="filtered-products"></div>'; // Container for filtered products
return $output;
}
// Shortcode registration
function custom_attribute_filter_shortcode( $atts ) {
$atts = shortcode_atts( array(
'attribute' => 'color',
), $atts, 'custom_attribute_filter' );
return get_custom_attribute_filter_form( $atts['attribute'] );
}
add_shortcode( 'custom_attribute_filter', 'custom_attribute_filter_shortcode' );
// Adding style and JavaScript
function custom_attribute_filter_scripts() {
?>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
var select = document.querySelector('#custom-attribute-filter select');
if (select) {
select.addEventListener('change', function() {
var attribute = this.name;
var value = this.value;
var data = {
action: 'filter_products',
attribute: attribute,
value: value,
};
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
document.getElementById('filtered-products').innerHTML = response;
});
});
}
});
</script>
<?php
}
add_action( 'wp_footer', 'custom_attribute_filter_scripts' );
// AJAX function to filter products
function ajax_filter_products() {
$attribute = sanitize_text_field( $_POST['attribute'] );
$value = sanitize_text_field( $_POST['value'] );
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'visible',
'compare' => '=',
),
),
);
if ( $value ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'pa_' . $attribute,
'field' => 'slug',
'terms' => $value,
),
);
}
$query = new WP_Query( $args );
ob_start(); // Start buffering the output
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
wc_get_template_part( 'content', 'product' );
}
} else {
echo __( 'No products found', 'text_domain' );
}
$output = ob_get_clean(); // Finish buffering and save the output to a variable
// Return the filter result as an AJAX response
wp_send_json_success( $output );
wp_die();
}
add_action( 'wp_ajax_filter_products', 'ajax_filter_products' );
add_action( 'wp_ajax_nopriv_filter_products', 'ajax_filter_products' );
?>`
New contributor
Marharyta Mukhina is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.