通过显示产品的AJAX问题按类别过滤产品

时间:2019-05-28 13:38:28

标签: wordpress woocommerce

我在显示带有Ajax类别的产品时遇到问题。 我已经渲染了所有类别和页面上的所有产品,并且我的类别是链接,并且我禁止对链接执行默认操作,而我想实现这一目标。当您单击类别的链接时,该链接会触发ajax并显示该类别的波纹管产品,所有操作均在同一页面上发生,而无需重新加载。但是当我单击类别链接时,我得到的只是“找不到帖子”

function ajax_filter_get_posts( $taxonomy ) {

    // Verify nonce
    if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
      die('Permission denied');

    $taxonomy = $_POST['taxonomy'];

    // WP Query
    $args = array(
      'tag' => $taxonomy,
      'post_type' => 'product',
      'posts_per_page' => 10

    );


    if( !$taxonomy ) {
      unset( $args['tag'] );
    }

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

      $output  = '<h2><a href="'.get_permalink().'">'. get_the_title().'</a></h2>';
      $output .= get_the_excerpt();

      $result = 'success';

    endwhile; else:
      $output = '<h2>No posts found</h2>';
      $result = 'fail';

    endif;

    $response = json_encode($output);
    echo $response;

    die();
  }

  add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
  add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
<article>
<?php
/**
 * Template name: AJAX Post Filter by Taxonomy
 *
 */
get_header();
$args = array(
    'post_type' => 'product',
    'posts_per_page' => 10
);

$query = new WP_Query( $args );

$tax = 'product_cat';
$terms = get_terms( $tax );
$count = count( $terms );

if ( $count > 0 ): ?>
    <div class="post-tags">
    <?php
        echo '<div class="container">';
        echo '<div class="row">';
        foreach ( $terms as $term ) {
         $term_link = get_term_link( $term, $tax );

         echo '<div class="col-4">';
        echo '<a href="' . $term_link . '" class="tax-filter" title="' . $term->slug . '">' . $term->name . '</a> ';
        echo '</div>';

     }
     echo '</div>';
        echo '</div>';
     ?>
    </div>
<?php endif;

if ( $query->have_posts() ): ?>
<div class="tagged-posts">
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>

    <h2><a class="<?php echo $term->name; ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>

    <?php endwhile; ?>
</div>

<?php else: ?>
    <div class="tagged-posts">
        <h2>No posts found</h2>
    </div>
<?php endif;  ?>

</article>
<?php
get_footer();
jQuery(document).ready(function( $ ) {
    $('.tax-filter').click( function(event) {


        if (event.preventDefault) {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }

        var selecetd_taxonomy = $(this).attr('title');

        $('.tagged-posts').fadeOut();

        data = {
            action: 'filter_posts',
            afp_nonce: afp_vars.afp_nonce,
            taxonomy: selecetd_taxonomy,
        };

        $.ajax({
            type: 'product',
            dataType: 'json',
            url: afp_vars.afp_ajax_url,
            data: data,
            success: function( data, textStatus, XMLHttpRequest ) {
                $('.tagged-posts').html( data );
                $('.tagged-posts').fadeIn();
                console.log( textStatus );
                console.log( XMLHttpRequest );
            },
            error: function( MLHttpRequest, textStatus, errorThrown ) {
                console.log( MLHttpRequest );
                console.log( textStatus );
                console.log( errorThrown );
                $('.tagged-posts').html( 'No posts found' );
                $('.tagged-posts').fadeIn();
            }
        })

    });
});

1 个答案:

答案 0 :(得分:0)

在您的查询参数中,您使用tag进行过滤,而您传入​​的分类术语实际上是一个product_cat

尝试将ajax_filter_get_posts中的查询参数更改为以下内容:

$args = array(
  'post_type' => 'product',
  'posts_per_page' => 10
  'tax_query' => array(
    array (
      'taxonomy' => 'product_cat',
      'field' => 'slug',
      'terms' => $taxonomy,
    )
  ),
);