T
T
trenton2021-08-03 01:19:23
AJAX
trenton, 2021-08-03 01:19:23

How to display a message that there are none when deleting all posts/products added to the page?

Through php part or through js? There is an else at the bottom of the page, which displays a message if nothing was initially added there, but if everything added is deleted (and not originally visited the page when it is not there yet), then after deleting the last product-post, a message should be displayed that the list is empty. What is the right way to specify something like if ($wp_query->the_post() == 0)... in php before else, or in js in sucess to specify it somehow differently and add it as html?
Favorites page (not a plugin)

<?php
   /*
   Template Name: избранное
   */
   ?>
<?php get_header(); ?>
<div class="container-fluid">
   <section class="favorites">
      <div class="row">       
         <?php
            $favorite_id_array = favorite_id_array();
               global $wp_query;
               $save_wpq = $wp_query;
               $args = array(
               'paged' => get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1,
               'post_type'   =>  ['product'], 
               'posts_per_page'   =>  20,    
               'post__in'   => !empty($favorite_id_array) ? $favorite_id_array : array(0),
                );
               $wp_query = new WP_Query( $args ); 
               ?>
         <?php if ($wp_query->have_posts()) : ?>
         <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>            
         <div class="col-md-3 col-6 fav">
            <div class="fv_<?php echo $post->ID; ?>">
               <div class="delete-favorite" data-post_id="<?php echo $post->ID; ?>" title="Удалить из избранного">
                  <div href="#"><span class="fa-fullheart"></span></div>
               </div>
            </div>
            <a class="favdata" href="<?php the_permalink() ?>">
               <?php the_post_thumbnail('woocommerce_thumbnail'); ?>
               <div class="favdata-content">
               <h5><?php the_title(); ?></h5> 
                   <div class="favdata-content-price">
                     <?php  if (!empty($product)) {
                      echo $product->get_price_html();
                   }?></div> 
               </div>
            </a>
         </div>

         <?php endwhile; ?>
         <?php else : ?>
         <div class="col-md-8 col-12">Список избранного пуст</div>
         <?php endif; ?>
         <?php wp_reset_postdata();
          themename_blog_pagination();
            ?>
      </div>
   </section>
</div>
<?php 
   get_footer();


js part
jQuery(function($) {
  //добавить в избранное
  $('body').on('click', '.add-favorite', function() {
    var post_id = $(this).data('post_id');
    $.ajax({
      url: "/wp-admin/admin-ajax.php",
      type: 'POST',
      data: {
        'action': 'favorite',
        'post_id': post_id,
      },
      success: function(data) {
        $('.fv_' + post_id).html('<div class="favs wish-btn-fill d-flex align-items-center already-in"><a href="/favorite/"><span class="fa-fullheart"></span></a></div>');
        $('.num-favorite').html(data);
        
      },
    });
  });
  //удалить из избранного
  $('body').on('click', '.delete-favorite', function() {
    var post_id = $(this).data('post_id');       
    $.ajax({
      url: "/wp-admin/admin-ajax.php",
      type: 'POST',
      data: {
        'action': 'delfavorite',
        'post_id': post_id,
      },
      success: function(data) {
        $('.fv_' + post_id).html('Удалено').slideToggle();
        $('.fv_' + post_id).parent('.fav').slideToggle();
        $('.num-favorite').html(data);
      },
    });
  });
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question