This topic contains a post which is marked as Best Answer. Press here if you would like to see it.
*

FezMonki

  • **
  • 8 posts
Display number of times an item was favorited.
« on: August 13, 2021, 10:56:35 PM »
Hi, I was wondering if there is a way/shorthand to display the number of times an item was favorited on the items page, like "echo osc_item_views()" will show the views.

Thanks!

*

MB Themes

Re: Display number of times an item was favorited.
« Reply #1 on: August 16, 2021, 09:55:38 AM »
This could work:
Code: [Select]
<?php echo count(ModelFI::newInstance()->getUserListByItemId(osc_item_id())); ?>
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

FezMonki

  • **
  • 8 posts
Re: Display number of times an item was favorited.
« Reply #2 on: August 16, 2021, 02:09:33 PM »
Thank you, it does work as expected 50%, as it will only show the count of favorites if the list has notifications turned on. But thanks to you pointing me to it, I built a new function from two others in ModelFI.php, and this works as expected:

ModelFI.php:
public function getFavoriteCount( $item_id ) {
  $this->dao->select('a.*');
  $this->dao->from( $this->getTable_FavoriteItems() . ' a');
  $this->dao->where('a.item_id', $item_id );

  $result = $this->dao->get();
 
  if( !$result ) { return array(); }
  $prepare = $result->result();
  return $prepare;
}

item.php:
<?php echo count(ModelFI::newInstance()->getFavoriteCount(osc_item_id())); ?>

Thanks again!

Marked as best answer by frosticek on August 17, 2021, 03:05:03 PM
*

MB Themes

Re: Display number of times an item was favorited.
« Reply #3 on: August 17, 2021, 09:58:47 AM »
@FezMonki
If you want to code it, that is wrong solution as you want to put aggregation on database side.
Code: [Select]
public function getFavoriteCount( $item_id ) {
  $this->dao->select('count(a.*) as i_count');
  $this->dao->from( $this->getTable_FavoriteItems() . ' a');
  $this->dao->where('a.item_id', $item_id );

  $result = $this->dao->get();
 
  if( !$result ) { return 0; }
  $prepare = $result->row();
  return $prepare['i_count'];
}

Then:
Code: [Select]
<?php echo ModelFI::newInstance()->getFavoriteCount(osc_item_id()); ?>
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

FezMonki

  • **
  • 8 posts
Re: Display number of times an item was favorited.
« Reply #4 on: August 17, 2021, 02:37:43 PM »
Thank you very much, I'll go ahead and update my code.

*

Vettalo

  • ***
  • 43 posts
Re: Display number of times an item was favorited.
« Reply #5 on: September 13, 2021, 11:59:44 PM »
Hi, I was wondering if there is a way/shorthand to display the number of times an item was favorited on the items page, like "echo osc_item_views()" will show the views.

Thanks!

database create file and name it anything.sql and import it to database and change oc_ with the database prefix you are using

CREATE TABLE `oc_t_item_stats_fav` (
  `fk_i_item_id` int(11) UNSIGNED NOT NULL,
  `i_num_favorite_clicks` int(10) DEFAULT 0,
  `dt_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


ALTER TABLE `oc_t_item_stats_fav`
  ADD PRIMARY KEY (`fk_i_item_id`,`dt_date`);
COMMIT;



global.js add

   $('.fi_save_favorite').click(function(e){
    var itemId = $(this).attr('data-item-id');
   var favCount = $(this).attr('data-favorite'); 
    if( $(this).attr('href') != '' ) {   
      window.location.href = $(this).attr('href');
    } else {
      $(this).attr('href', favCount);
      $.ajax({
        url: baseAjaxUrl + "&ajaxFavoClick=1&itemId=" + itemId,
        type: "GET",
        success: function(response){
          //console.log(response);
        }
      });
    }
  });

 
function.php in your theme add
 
  // INCREASE FAV CLICK VIEWS
function fav_increase_clicks($itemId, $itemUserId = NULL) {
  if($itemId > 0) {
    if($itemUserId == '' || $itemUserId == 0 || ($itemUserId <> '' && $itemUserId > 0 && $itemUserId <> osc_logged_user_id())) {
      $db_prefix = DB_TABLE_PREFIX;
      //$query = "INSERT INTO {$db_prefix}t_item_stats_fav (fk_i_item_id, dt_date, i_num_favorite_clicks) VALUES ({$itemId}, \"{date('Y-m-d')}\", 1) ON DUPLICATE KEY UPDATE  i_num_favorite_clicks = i_num_favorite_clicks + 1";
      $query = 'INSERT INTO ' . $db_prefix . 't_item_stats_fav (fk_i_item_id, dt_date, i_num_favorite_clicks) VALUES (' . $itemId . ', "' . date('Y-m-d') . '", 1) ON DUPLICATE KEY UPDATE  i_num_favorite_clicks = i_num_favorite_clicks + 1';
      return ItemStats::newInstance()->dao->query($query);
    }
  }
}

// INCREASE FAV CLICK VIEWS
function favorite_clicks( $item_id ) {
  if( $item_id <> '' ) {
    $db_prefix = DB_TABLE_PREFIX;

    $query = "SELECT sum(coalesce(i_num_favorite_clicks, 0)) as favorite_clicks FROM {$db_prefix}t_item_stats_fav s WHERE fk_i_item_id = " . $item_id . ";";
    $result = ItemStats::newInstance()->dao->query( $query );
    if( !$result ) {
      $prepare = array();
      return '0';
    } else {
      $prepare = $result->row();

      if($prepare['favorite_clicks'] <> '') {
        return $prepare['favorite_clicks'];
      } else {
        return '0';
      }
    }
  }
}



ajax.php in your theme add

// INCREASE CLICK COUNT ON FAVORITE
if(isset($_GET['ajaxFavoClick']) && $_GET['ajaxFavoClick'] == '1' && isset($_GET['itemId']) && $_GET['itemId'] > 0) {
  fav_increase_clicks($_GET['itemId'], $_GET['itemUserId']);
  echo 1;
}


function.php in favorite plugin

replace
  $text .= '<a href="javascript://" class="fi_make_favorite fi_make fi_fav_' . $item_id . $class . '" rel="' . $item_id . '" title="' . osc_esc_html($title) . '">';
with
  $text .= '<a href="" data-favorite="javascript://" data-item-id="' . $item_id . '" class="fi_make_favorite fi_make fi_fav_' . $item_id . $class . '" rel="' . $item_id . '" title="' . osc_esc_html($title) . '">';

now display how many time item has been added to favorite in your item.php

<?php echo favorite_clicks(osc_item_id()); ?> <?php echo (favorite_clicks(osc_item_id()) == 0 ? __('Favorite', 'theme name') : __('Favorites', 'theme name')); ?>

*

MB Themes

Re: Display number of times an item was favorited.
« Reply #6 on: September 14, 2021, 01:12:53 PM »
Thanks for nice guide ;)
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

Vettalo

  • ***
  • 43 posts
Re: Display number of times an item was favorited.
« Reply #7 on: September 14, 2021, 05:58:02 PM »
Thanks for nice guide ;)
You are welcome  ;)