Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* --------------------------------------
- * 12. Love It Button
- * -------------------------------------- */
- $('.thm-love-btn').on('click', function(e) {
- e.preventDefault();
- var that = $(this);
- var campaign_id = that.data('campaign');
- var user_id = that.data('user');
- if( user_id != 0 && campaign_id ){
- $.ajax({
- type:"POST",
- url: ajax_objects.ajaxurl,
- data: {'action': 'thm_campaign_action', 'campaign_id': campaign_id},
- success:function(data){
- data = JSON.parse(data);
- if (data.success == 1){
- that.find('.amount').html(data.number);
- if( data.message == 'love' ){
- that.addClass( 'active' ).parents('.themeum-campaign-post').find('.themeum-campaign-img').addClass('active');
- }else{
- that.removeClass( 'active' ).parents('.themeum-campaign-post').find('.themeum-campaign-img').removeClass('active');
- }
- }
- }
- });
- }else{
- $('#myModal').modal('show');
- }
- });
- ========================================
- <a href="#" class="thm-love-btn <?php echo esc_attr($active); ?>" data-campaign="<?php echo get_the_ID(); ?>" data-user="<?php echo get_current_user_id(); ?>">
- <i class="fa fa-heart-o"></i>
- </a>
- ====================================
- <!-- <i class="back-heart"></i> -->
- <span class="thm-Price-amount text-center">
- <span class="thm-love-btn" data-campaign="<?php echo get_the_ID(); ?>" data-user="<?php echo get_current_user_id(); ?>">
- <i class="back-heart"></i>
- <span class="woocommerce-Price-amount amount latest-price">
- <?php
- $love_count = get_post_meta( get_the_ID(),'loved_campaign_ids', true );
- if( $love_count ){
- echo esc_attr($love_count);
- }else{
- echo '0';
- }
- ?>
- </span>
- <span class="thm-raise-sp"><?php _e('Love it', 'backnow') ?></span>
- </span>
- </span>
- =========================================
- function.php
- /* -------------------------------------------
- * Love it Action
- * ------------------------------------------- */
- add_action( 'wp_ajax_thm_campaign_action','themeum_campaign_action' );
- add_action( 'wp_ajax_nopriv_thm_campaign_action', 'themeum_campaign_action' );
- function themeum_campaign_action(){
- if ( ! is_user_logged_in()){
- die(json_encode(array('success'=> 0, 'message' => __('Please Sign In first', 'backnow') )));
- }
- $loved_campaign_ids = array();
- $user_id = get_current_user_id();
- $campaign_id = sanitize_text_field($_POST['campaign_id']);
- $prev_campaign_ids = get_user_meta($user_id, 'loved_campaign_ids', true);
- $postid = get_post_meta( $campaign_id, 'loved_campaign_ids', true );
- if ($prev_campaign_ids){
- $loved_campaign_ids = json_decode( $prev_campaign_ids, true );
- }
- if (in_array($campaign_id, $loved_campaign_ids)){
- if(($key = array_search($campaign_id, $loved_campaign_ids)) !== false) {
- unset( $loved_campaign_ids[$key] );
- }
- $json_update_campaign_ids = json_encode($loved_campaign_ids);
- update_user_meta($user_id, 'loved_campaign_ids', $json_update_campaign_ids);
- if( $postid ){
- $postid = (int)$postid - 1;
- update_post_meta( $campaign_id, 'loved_campaign_ids', $postid );
- }else{
- $postid = 0;
- update_post_meta( $campaign_id, 'loved_campaign_ids', 0 );
- }
- die(json_encode(array('success'=> 1, 'number' => $postid, 'message' => 'delete' )));
- }else{
- $loved_campaign_ids[] = $campaign_id;
- update_user_meta($user_id, 'loved_campaign_ids', json_encode($loved_campaign_ids) );
- if( $postid ){
- $postid = (int)$postid + 1;
- update_post_meta( $campaign_id, 'loved_campaign_ids', $postid );
- }else{
- $postid = 1;
- update_post_meta( $campaign_id, 'loved_campaign_ids', 1 );
- }
- die(json_encode(array('success'=> 1, 'number' => $postid , 'message' => 'love' )));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement