Php WP_Query not";“重置”;当AJAX过滤器参数变为空时
这个问题是,但由于某些原因被认为是离题的,所以我根据裁决意见中的建议在这里发布 我已经创建了一个AJAX分类(Php WP_Query not";“重置”;当AJAX过滤器参数变为空时,php,ajax,wordpress,Php,Ajax,Wordpress,这个问题是,但由于某些原因被认为是离题的,所以我根据裁决意见中的建议在这里发布 我已经创建了一个AJAX分类(product\u cat)过滤器,用于我的WooCommerce产品归档页面(archive product.php)。页面加载正常的WooCommerce商店循环onload: woocommerce_product_loop_start(); if (wc_get_loop_prop('total')) { while (have_posts()) { t
product\u cat
)过滤器,用于我的WooCommerce产品归档页面(archive product.php
)。页面加载正常的WooCommerce商店循环onload:
woocommerce_product_loop_start();
if (wc_get_loop_prop('total')) {
while (have_posts()) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*
* @hooked WC_Structured_Data::generate_product_data() - 10
*/
do_action('woocommerce_shop_loop');
wc_get_template_part('content', 'product');
}
}
woocommerce_product_loop_end();
该页面正确加载,显示所有产品。过滤器也工作得很好。它可以正确过滤一个或多个分类
问题是当我取消选中所有复选框时。我希望它清除任何过滤器,并再次显示所有产品。相反,我当前收到一个错误:
POST .../wp-admin/admin-ajax.php 500
相关产品过滤器:
function handleFilterCheckboxesChange(event) {
const data = {
action: 'wpf_update_products',
filters: get_current_filters()
};
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
data: data,
success: function (res) {
console.log("AJAX Success: ", data.filters);
// > ["example_category_slug"]
$('.products').html(res);
},
error: function (err) {
console.error("AJAX Error: ", data.filters);
// > []
}
});
$(document.body).trigger('post-load');
}
function get_current_filters() {
const checkboxes = $('.product-cat-filter-checkbox').get();
let checked = [];
for (let i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
checked.push(checkboxes[i].id);
};
}
return checked;
}
…从同一类钩住:
add_action('wp_ajax_wpf_update_products', array($this, 'wpf_update_products'));
add_action('wp_ajax_nopriv_wpf_update_products', array($this, 'wpf_update_products'));
当我在成功筛选时var_转储查询时,它正确地显示了增值税_查询,但我无法在不成功的[empty]筛选时转储查询,因为服务器响应为500
如果我在服务器端处理空的$\u POST['filters']
,则无法找出返回错误的原因。有人能看到我遗漏了什么吗
更新:我在服务器端查询中添加了一个else,并且能够绕过500错误。用于设置筛选器查询参数的更新的if
语句为:
if ($filter_terms !== null && isset($filter_terms) && $filter_terms !== '' && !empty($filter_terms)) {
$args['tax_query'][] = [
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $filter_terms
];
} else {
$args['tax_query'][] = [
'taxonomy' => 'product_cat'
];
}
现在,查询尝试运行,但我得到了“对不起,没有适合您的查询的产品。”
语句,因为找不到帖子WP\u查询
似乎不喜欢只有分类设置的tax\u查询
。I var_转储了该查询,并在选中筛选器时查看tax_查询
是否正确填充了三个参数:
'tax_query' =>
array (size=1)
0 =>
array (size=3)
...
…当所有过滤器都取消选中时,tax\u查询
具有一个'taxonomy'=>'product\u cat'
参数:
'tax_query' =>
array (size=1)
0 =>
array (size=1)
...
如果过滤器为空,我似乎无法清除tax\u查询。使用原始代码noelse
语句if filters为空—我刚刚得到了500个错误,但即使我的else语句将tax\u查询设置为完全空,也会像这样:$args['tax\u query'][]=[]
,它仍然抛出500个错误。如果tax\u query
为空,为什么WP不返回到原始查询的其余部分
最后,我还尝试将查询参数手动设置为所有可能的分类术语,如下所示:
if ($filter_terms !== null && isset($filter_terms) && $filter_terms !== '' && !empty($filter_terms)) {
$args['tax_query'][] = [
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $filter_terms
];
} else {
$all_terms = get_terms(array('taxonomy' => 'product_cat', 'fields' => 'slugs'));
$args['tax_query'][] = [
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $all_terms
];
}
…但我又得到了500个错误:/
倾销$all_terms
为我提供了包含任何产品的所有术语的列表:
array (size=4)
0 => string 'bakeware' (length=8)
1 => string 'cookware' (length=8)
2 => string 'cutlery-kitchen-tools' (length=21)
3 => string 'linens' (length=6)
我没有看到在wpf\u update\u products()
函数的范围内设置$filter\u terms
变量。如果我正确理解了代码的意图,请尝试以下操作:
// I expect the next line to check for empty filter terms and "reset"/skip this tax_query, but I still get a 500 error
if ( ! empty( $_POST['filters'] ) ) {
$args['tax_query'][] = [
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_POST['filters']
];
}
谢谢你的回答!我道歉。我在实际代码中设置了$filter\u terms
,但在我写问题时,它并没有转移到SO。我已经更新了上面的代码。你可以看到我有!空($\u POST['filters'])
也检查原因。还有其他想法吗?我有一些预感。假设您使用的是PHP7+,请尝试如下设置:$filter\u terms=$\u POST['filter\u terms']??无效代码>这是一个很长的机会,但是当触发PHP通知时,您的服务器可能会触发500个错误。如果不起作用,请打印($filter\u terms)代码>未选中任何复选框时,将输出发送给我。抱歉-在最后一条评论中键入错误<代码>$filter\u terms=$\u POST['filters']??无效代码>所以,它似乎不起作用。用??无效
您建议,当检查过滤器时,我仍然会得到此打印结果:数组([0]=>亚麻布)
但我无法打印($filter\u terms)因为服务器仍然返回500错误而取消选中时,代码>。是的,我使用的是PHP7.4.1。我用一些新的尝试/发现更新了上面的问题。
// I expect the next line to check for empty filter terms and "reset"/skip this tax_query, but I still get a 500 error
if ( ! empty( $_POST['filters'] ) ) {
$args['tax_query'][] = [
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_POST['filters']
];
}