2021年3月21日星期日

How to make both wishlist and compare list based on cookies work together?

I use wishlist, based on cookies, and want to make the same for compare, only modify a little, like showing attributes in the compare page. I love exactly that way. Don't want plugins. I'm copying all and change 'favorite' to 'compare, 'fav' to 'cmp' and 'fv' to 'cm', also 'delfavorite' to 'delcompare'. Functions from original wishlist.

function favorite_id_array() {       if (!empty( $_COOKIE['favorite_post_ids'])) {          return explode(',', $_COOKIE['favorite_post_ids']);      }      else {          return array();      }  }    function add_favorite() {      $post_id = (int)$_POST['post_id'];      if (!empty($post_id)) {          $new_post_id = array(              $post_id          );          $post_ids = array_merge($new_post_id, favorite_id_array());          $post_ids = array_diff($post_ids, array(              ''          ));          $post_ids = array_unique($post_ids);          setcookie('favorite_post_ids', implode(',', $post_ids) , time() + 3600 * 24 * 365, '/');          echo count($post_ids);      }      die();  }  add_action('wp_ajax_favorite', 'add_favorite');  add_action('wp_ajax_nopriv_favorite', 'add_favorite');    function delete_favorite() {      $post_id = (int)$_POST['post_id'];      if (!empty($post_id)) {          $favorite_id_array = favorite_id_array();          if (($delete_post_id = array_search($post_id, $favorite_id_array)) !== false) {              unset($favorite_id_array[$delete_post_id]);          }          setcookie('favorite_post_ids', implode(',', $favorite_id_array) , time() + 3600 * 24 * 30, '/');          echo count($favorite_id_array);      }      die();  }  add_action('wp_ajax_delfavorite', 'delete_favorite');  add_action('wp_ajax_nopriv_delfavorite', 'delete_favorite');  

js

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><span class="fav-text">In wishlist</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('Deleted').slideToggle();          $('.fv_' + post_id).parent('.fav').slideToggle();          $('.num-favorite').html(data);        },      });    });  });  

favorite page

<?php     /*     Template Name: favorite     */     ?>  <?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'   =>  ['post','product_variation','product'],                    '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="Delete from wishlist">                    <div href="#"><span class="fa-trash"></span>Delete</div>                 </div>              </div>              <a class="favdata" href="<?php the_permalink() ?>">                 <?php the_post_thumbnail('grid'); ?>                 <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 text-center">Your Wishlist is Empty</div>           <?php endif; ?>           <?php wp_reset_postdata();               themename_blog_pagination();              ?>        </div>     </section>  </div>  <?php      get_footer();  

But when I'm trying to add product to compare list, it doesn't work, and console says, that's something wrong with ajax. it says nothing about what's exactly wrong, admin-ajax shows 0 and that's all. showing in woocommerce product cart

add_action( 'woocommerce_after_add_to_cart_button', 'themename_single_product_add_to_wishlist',35);  function themename_single_product_add_to_wishlist() {    global $post;        if( in_array($post->ID, favorite_id_array())){         ?>     <div class=" favs wish-btn-fill d-flex align-items-center fv_<?php echo $post->ID; ?>" title="Already in wishlist" >          <a href="/favorite/"><span class="fa-fullheart"></span>In wishlist</a>    </div>     <?php } else { ?>     <div class="fv_<?php echo $post->ID; ?>" >        <div class="add-favorite" title="Add to wishlist" data-post_id="<?php echo $post->ID; ?>">           <span class="fa-heart"></span>Add to wishlist        </div>     </div>                       <?php    }   }  

What can be wrong? And how to make it work right? The action is wrong?

https://stackoverflow.com/questions/66739152/how-to-make-both-wishlist-and-compare-list-based-on-cookies-work-together March 22, 2021 at 09:04AM

没有评论:

发表评论