// AJAX handler for finding products by tire size with enhanced checks and fallback
add_action('wp_ajax_find_products_by_tire_size', 'find_products_by_tire_size');
add_action('wp_ajax_nopriv_find_products_by_tire_size', 'find_products_by_tire_size');
function find_products_by_tire_size() {
verify_ajax_nonce();
if (isset($_POST['tire_size'])) {
$tire_size = sanitize_text_field($_POST['tire_size']);
// Primary query with both taxonomy slugs
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'pa_tire_size',
'field' => 'name',
'terms' => $tire_size,
),
array(
'taxonomy' => 'pa_tire-size',
'field' => 'name',
'terms' => $tire_size,
),
),
);
$query = new WP_Query($args);
// Check if the query returned any products
if ($query->have_posts()) {
$html = '';
while ($query->have_posts()) {
$query->the_post();
$html .= "
" . get_the_title() . "
";
}
wp_reset_postdata();
wp_send_json_success($html);
} else {
// Logging and fallback for additional insights
error_log("No products found for tire size: {$tire_size}. Attributes checked: pa_tire_size, pa_tire-size");
// Fallback query without tax_query to see if any products load
$fallback_args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
$fallback_query = new WP_Query($fallback_args);
if ($fallback_query->have_posts()) {
wp_send_json_error("Products exist, but none match the specified tire size taxonomy filter. Check the tire size taxonomy.");
} else {
wp_send_json_error("No products available at all, verify WooCommerce product data.");
}
wp_reset_postdata();
}
} else {
wp_send_json_error("Tire size not provided.");
}
}
Warning: Cannot modify header information - headers already sent by (output started at /home/goddealca/public_html/wp-content/themes/hello-elementor-child/functions.php:1) in /home/goddealca/public_html/wp-includes/pluggable.php on line 1435
Warning: Cannot modify header information - headers already sent by (output started at /home/goddealca/public_html/wp-content/themes/hello-elementor-child/functions.php:1) in /home/goddealca/public_html/wp-includes/pluggable.php on line 1438