0
我爲我的WooCommerce產品創建了一個AJAX過濾器。當我使用工具集類型時,這是工作的,但現在不使用CPT UI(自定義帖子類型UI)創建自定義分類。當我過濾時,它會起作用,但所有選項都不起作用。我甚至創建了一個全新的安裝來測試它。WordPress的AJAX過濾器與CPT用戶界面和多種taxonmy
所以,當我選擇熱量分類項目(它在該選項帶來了項目),然後選擇一餐類型分類,它帶來了項目只在用餐類型選項,然後當我選擇了過敏它只在該餐類中提供物品的分類。
我希望他們一起工作,以便點擊每個項目將其縮小並顯示屬於全部選定選項的項目。
的functions.php
// WooCommerce AJAX Filter
function my_filters(){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'asc',
);
if(isset($_POST['caloriefilter']))
$args['tax_query'] = array(
array(
'taxonomy' => 'z_calories',
'field' => 'id',
'terms' => $_POST['caloriefilter']
),
);
if(isset($_POST['mealtypefilter']))
$args['tax_query'] = array(
array(
'taxonomy' => 'z_meal_type',
'field' => 'id',
'terms' => $_POST['mealtypefilter']
),
);
if(isset($_POST['ingredientfilter']))
$args['tax_query'] = array(
array(
'taxonomy' => 'z_ingredients',
'field' => 'id',
'operator' => 'NOT IN',
'terms' => $_POST['ingredientfilter']
),
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()): $query->the_post();
wc_get_template_part('content', 'product');
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
}
add_action('wp_ajax_customfilter', 'my_filters');
add_action('wp_ajax_nopriv_customfilter', 'my_filters');
頁面title.php
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<h2>Calories</h2>
<div class="divider div-transparent"></div>
<?php
if($terms = get_terms(array(
'taxonomy' => 'z_calories',
'hide_empty' => false,
'orderby' => 'none',
))) :
echo '<div class="row category-buttons">';
foreach ($terms as $term) :
echo '<div class="col-12 col-sm col-md"><input type="checkbox" id="meal-calorie-' . $term->term_id . '" value="' . $term->term_id . '" name="caloriefilter[]" /><label for="meal-calorie-' . $term->term_id . '">' . $term->name . '</label></div>';
endforeach;
echo '</div>';
endif;
?>
<h2>Meal Type</h2>
<div class="divider div-transparent"></div>
<?php
if($terms = get_terms(array(
'taxonomy' => 'z_meal_type',
'hide_empty' => false,
'orderby' => 'none',
))) :
echo '<div class="row category-buttons">';
foreach ($terms as $term) :
echo '<div class="col-12 col-sm col-md"><input type="checkbox" id="meal-type-' . $term->term_id . '" value="' . $term->term_id . '" name="mealtypefilter[]" /><label for="meal-type-' . $term->term_id . '">' . $term->name . '</label></div>';
endforeach;
echo '</div>';
endif;
?>
<a id="ingredientsToggle" class="clearfix" data-toggle="collapse" href="#ingredientsArea" aria-expanded="false" aria-controls="ingredientsArea"><button>Click for Allergies</button><h2>Allergies</h2></a>
<div class="collapse" id="ingredientsArea">
<div class="divider div-transparent"></div>
<p>Select items which you would like to avoid.</p>
<?php
if($terms = get_terms(array(
'taxonomy' => 'z_ingredients',
'hide_empty' => false,
'orderby' => 'name'
))) :
echo '<ul class="ingredients-form">';
foreach ($terms as $term) :
echo '<li><input type="checkbox" name="ingredientfilter[]" id="ingredients-' . $term->term_id . '" value="' . $term->term_id . '" /><label for="ingredients-' . $term->term_id . '"><div><i class="fa fa-square-o fa-fw fa-2x"></i><i class="fa fa-check-square-o fa-fw fa-2x"></i></div>' . $term->name . '</label></li>';
endforeach;
echo '</ul>';
endif;
?>
</div>
<input type="hidden" name="action" value="customfilter">
</form><!-- END Filter Form -->
javascript.js
/* Custom Shop Filter */
jQuery(function($){
$('#filter input').on('change', function() {
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
$('#loadingNotice').css('display' , 'block'); },
success:function(data){
$('#loadingNotice').css('display' , 'none');
$('#response').html(data);
}
});
return false;
});
});