0

exclude previously displayed products while sorting random and navigating to next page

i m using

if($this->getCurrentOrder() != 'price'){
        if($this->getCurrentOrder() != 'created_at') {
            $this->_collection->getSelect()->order('rand()');
        }else{
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }
}else{
        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}

in toolbar.php to display random items in product page but the products keep repeating !!

1
  • got my answer ! just want to ask how to stop repeated products during pagination while appling random sort to the product list Commented Mar 18, 2014 at 10:06

1 Answer 1

2

My thoughts on this approach

First of all I want to say that I disagree with the random order of products in a shop.
It is bad for business because you cannot promote certain products if you want and is bad for user experience.
Here is an example.
I see a list of products and give the url to someone and say "look at the second and forth product on this list". They will see a totally different thing than I do.
Not to mention that the random sorting is slow. (well..I actually mentioned it)

Possible solution

but if you insist here is a possible solution.
You need to order the products by a 'seeded' random order and make sure the 'seed' is the same all through out the session.

Here is a possible approach. I haven't tested it, but I've used this kind of seeded random sorting for listing other entities.

if($this->getCurrentOrder() != 'price'){
    if($this->getCurrentOrder() != 'created_at') {
        //check if there is a seed in the session.
        $seed = Mage::getSingleton('catalog/session')->getListSeed();
        //if there is none, generated it and store it in the session
        if (!$seed) {
            $seed = time(); //can replace with custom logic if you want
            Mage::getSingleton('catalog/session')->setListSeed($seed);
        }
        //get a random number based on seed
        srand($seed);
        $rand = rand(0, 100);
        //pass the random number to the sorting.
        $this->_collection->getSelect()->order('rand('.$rand.')');
    }
    else{
        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }
}
else {
        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
5
  • "srand($seed); $rand = rand(0, 100); " is it $rand or srand how are you passing the value of $seed to rand as we are giving it a range 0 -100 Commented Mar 18, 2014 at 9:13
  • @wittyvishwas. php.net/manual/en/function.srand.php
    – Marius
    Commented Mar 18, 2014 at 9:14
  • working perfectly fine !! thanks @marius !! Commented Mar 18, 2014 at 9:56
  • I just came across this question and answer after a long time searching. I asked a duplicate of this. Could Marius post this answer on mine and I'll accept it? magento.stackexchange.com/questions/63870/…
    – MW Millar
    Commented Jun 12, 2015 at 8:12
  • @kaska. Instead of duplicating my answer, wouldn't it be better if I close your question as duplicate of this?
    – Marius
    Commented Jun 12, 2015 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.