Osclass Support Forums
Osclass plugin support => Favorite Items Plugin => Topic started by: FezMonki 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!
-
This could work:
<?php echo count(ModelFI::newInstance()->getUserListByItemId(osc_item_id())); ?>
-
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!
-
@FezMonki
If you want to code it, that is wrong solution as you want to put aggregation on database side.
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:
<?php echo ModelFI::newInstance()->getFavoriteCount(osc_item_id()); ?>
-
Thank you very much, I'll go ahead and update my code.
-
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')); ?>
-
Thanks for nice guide ;)
-
Thanks for nice guide ;)
You are welcome ;)
-
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')); ?>
I did everything as you described but something does not work.
If anyone has an up-to-date solution for displaying the number of clicks in favorites, please share.