*

Arreff

  • **
  • 9 posts
Random listings for front page
« on: October 17, 2022, 04:36:43 AM »
Hi,
I am looking to randomize listings on the front page.
I hear I can do a subquery of random ids and then show those ids.

But I am not sure what the script should look like or which file I should put it in.
If anyone can shed some light on this, I would be grateful.

Cheers

*

Arreff

  • **
  • 9 posts
Re: Random listings for front page
« Reply #1 on: October 22, 2022, 03:59:53 PM »
I am guessing it has something to with this section of code from
osclass\oc-includes\osclass\helpers\hItems.php

at line 1067

Code: [Select]

/**

Gets next item of latest items query

@return boolean It returns true if there is another item available or false if there isn't
*/
function osc_has_latest_items($total_latest_items = null, $options = array(), $withPicture = false) {
// if we don't have the latest items loaded, do the query
if ( !View::newInstance()->_exists('latestItems') ) {
$search = Search::newInstance();
if( !is_numeric($total_latest_items) ) {
$total_latest_items = osc_max_latest_items();
}

View::newInstance()->_exportVariableToView('latestItems', $search->getLatestItems($total_latest_items, $options, $withPicture));
}

// keys we want to erase from View
$to_erase = array('resources', 'item_category', 'metafields');
foreach($to_erase as $t) {
if ( View::newInstance()->_exists($t) ) {
View::newInstance()->_erase($t);
}
}

// set itemLoop to latest if it's the first time we enter here
if(View::newInstance()->_get('itemLoop') !== 'latest') {
View::newInstance()->_exportVariableToView('oldItem', View::newInstance()->_get('item'));
View::newInstance()->_exportVariableToView('itemLoop', 'latest');
}

// get next item
$item = View::newInstance()->_next('latestItems');

if(!$item) {
View::newInstance()->_exportVariableToView('item', View::newInstance()->_get('oldItem'));
View::newInstance()->_exportVariableToView('itemLoop', '');
} else {
View::newInstance()->_exportVariableToView('item', View::newInstance()->_current('latestItems'));
}

// reset the loop once we finish just in case we want to use it again
if( !$item && View::newInstance()->_count('latestItems') > 0 ) {
View::newInstance()->_reset('latestItems');
}

return $item;
}

Should I post this somewhere else?

*

MB Themes

Re: Random listings for front page
« Reply #2 on: October 23, 2022, 08:39:57 PM »
@Arreff
You cannot do that with simple modifications, it is much more complex change.
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

cartagena68

  • ***
  • 71 posts
Re: Random listings for front page
« Reply #3 on: October 24, 2022, 12:34:11 AM »
You can try to add this code to the function.php in your theme folder

Code: [Select]
function special_main_all_listing_random() {
    $mSearch =  Search::newInstance();
    $query_elements = (array) json_decode($mSearch->toJson());

    $mSearch->addField(sprintf("(SELECT RAND() FROM %st_item i2 WHERE %st_item.pk_i_id = i2.pk_i_id) AS rand_main",  DB_TABLE_PREFIX, DB_TABLE_PREFIX, DB_TABLE_PREFIX));
    $mSearch->order("rand_main DESC, " . $query_elements['order_column'], $query_elements['order_direction']);
}
if(osc_is_home_page()){
osc_add_hook('init', 'special_main_all_listing_random');
}