我正在尝试为我的WooCommerce商店页面制作一个AJAX产品过滤器。之前,我自己创建了查询,没有使用商店页面。然而,我想让我的代码现在更兼容WC。
目前,我可以从查询字符串中设置变量来过滤WooCommerce产品循环。
我使用以下代码执行此操作:
add_action(\'pre_get_posts\', \'filter_pre_get_posts\' );
function filter_pre_get_posts( $wp_query ) {
if(is_shop()){
$filter_term = $_GET[\'brand\'];
if (isset($filter_term ) && !empty($filter_term )) {
$wp_query->set(\'tax_query\', array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'pa_branding\',
\'field\' => \'slug\',
\'terms\' => $filter_term ,
\'include_children\' => true,
\'operator\' => \'IN\'
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $filter_term ,
\'include_children\' => true,
\'operator\' => \'IN\'
),
array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => $filter_term ,
\'include_children\' => true,
\'operator\' => \'IN\'
)
));
}
}
}
现在,我希望在单击复选框时能够执行相同的操作。
单击复选框时,Javascript会将复选框的值添加到具有数组的对象中:
var object = {
\'tags\' : [],
\'priceRange\' : [],
};
$(\'.checkbox-tag\').change(function() {
if($(this).prop("checked") == true){
object.tags.push($(this).val());
} else if($(this).prop("checked") == false) {
var x = object.tags.indexOf($(this).val());
object.tags.splice(x,1);
}
console.log(object);
var formData = object;
aJaxFilter(formData);
});
$(\'.checkbox-price\').click(function() {
if($(this).prop("checked") == true){
var string = ($(this).val());
object.priceRange = string.match(/\\d+/g);
}
console.log(object);
var formData = object;
aJaxFilter(formData);
});
然后我使用Ajax发送这个对象。
function aJaxFilter(formData) {
$.ajax({
url: js_variables.ajaxURL,
type:\'GET\',
data: {
\'formData\' : formData,
\'action\' : \'listFilteredProducts\'
},
success:function(data){
console.log(data);
$(\'.chd-shop-product-loop .products\').html(data);
}
});
}
我现在要做的是更改产品循环,就像我使用第一段代码中的参数对wp\\u query->set()所做的那样。
我试图在Ajax函数中遵循->
add_action(\'wp_ajax_listFilteredProducts\', \'listFilteredProducts\');
add_action(\'wp_ajax_nopriv_listFilteredProducts\', \'listFilteredProducts\');
function listFilteredProducts($wp_query) {
if(isset($_GET[\'formData\'])) {
$value = $_GET[\'formData\'];
}
// If no price input, but tags have been input
if ($value[\'priceRange\'] == 0 && $value[\'tags\'] != 0) {
$wp_query->set(\'tax_query\', array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
array(
\'taxonomy\' => \'pa_branding\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
));
//If price has been input, but no tags have been input
} else if ($value[\'priceRange\'] != 0 && $value[\'tags\'] == 0) {
$wp_query->set(\'meta_query\', array(
array(
\'key\' => \'_price\',
\'value\' => array($value["priceRange"][0], $value["priceRange"][1]),
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\'
)
));
//if neither price or tags input
} else if ($value[\'priceRange\'] == 0 && $value[\'tags\'] == 0) {
$wp_query->set(\'post_type\', \'product\');
// if tags and price input
} else if($value[\'priceRange\'] && $value[\'tags\']) {
$wp_query->set(\'tax_query\', array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
array(
\'taxonomy\' => \'pa_branding\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
));
$wp_query->set(\'meta_query\', array(
\'relation\' => \'AND\',
array(
\'key\' => \'_price\',
\'value\' => array($value["priceRange"][0], $value["priceRange"][1]),
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\'
)
));
}
wp_die();
}
SO网友:parvez noor
现在我明白了。
我没有使用$wp\\u query->set(),而是设置参数,然后运行woocommerce循环。这是我最后使用的代码。
add_action(\'wp_ajax_listFilteredProducts\', \'listFilteredProducts\');
add_action(\'wp_ajax_nopriv_listFilteredProducts\', \'listFilteredProducts\');
function listFilteredProducts($wp_query) {
if(isset($_GET[\'formData\'])) {
$value = $_GET[\'formData\'];
}
// If no price input, but tags have been input
if ($value[\'priceRange\'] == 0 && $value[\'tags\'] != 0) {
$args = array(
\'post_type\' => \'product\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
array(
\'taxonomy\' => \'pa_branding\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
)
);
//If price has been input, but no tags have been input
} else if ($value[\'priceRange\'] != 0 && $value[\'tags\'] == 0) {
$args = array(
\'post_status\' => \'publish\',
\'post_type\' => \'product\',
\'posts_per_page\' => -1,
\'meta_query\' => array(
array(
\'key\' => \'_price\',
\'value\' => array($value["priceRange"][0], $value["priceRange"][1]),
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\'
)
)
);
//if neither price or tags input
} else if ($value[\'priceRange\'] == 0 && $value[\'tags\'] == 0) {
if(!$value[\'priceRange\']) {
$value[\'priceRange\'] = array(0,1000000);
}
$args = array(
\'post_type\' => \'product\',
\'posts_per_page\' => -1,
);
// if tags and price input
} else if($value[\'priceRange\'] && $value[\'tags\']) {
$args = array(
\'post_type\' => \'product\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'product_tag\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
array(
\'taxonomy\' => \'pa_branding\',
\'field\' => \'slug\',
\'terms\' => $value[\'tags\'],
),
),
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'_price\',
\'value\' => array($value["priceRange"][0], $value["priceRange"][1]),
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\'
)
),
);
}
$ajaxposts = new WP_Query( $args );
$response = \'\';
if ( $ajaxposts->posts ){
while ( $ajaxposts->have_posts() ) {
$ajaxposts->the_post();
$response .= wc_get_template_part( \'content\', \'product\' ); // use WooCommerce function to get html
}
} else {
// handle not found by yourself or
// perhaps do_action( \'woocommerce_no_products_found\' ); could do the trick?
}
echo $response;
exit;
wp_die();
}