Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Show random products from a category

   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
Thread Tools Search this Thread
Old 11-17-2017, 07:01 AM
sabinudash sabinudash is offline

Advanced Member
Join Date: Jul 2007
Posts: 66

Default Re: Banner System Mod's HTML Banner

Would you please kindly guide how the script would look like to call products randomly from a specific category to display on a page?

Thank you in advance for your help.
X-Cart Multivendor
X-Cart Business (Testing)
Reply With Quote
Old 11-26-2017, 09:01 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline

Join Date: Dec 2010
Posts: 6,373

Default Re: Show random products from a category

You can extend your widget from \XLite\View\ItemsList\Product\Customer\Category\AC ategory class - this will allow you to show products from a particular category.

But the random order is not that easy. Yes, you can use "ORDER BY RAND()" by returning "RAND()" as the "order by" parameter in getSortByModeDefault() method. But MySQL is too slow when doing this on more than 100 products. So, you will have to implement some tricks to speed up random queries.
Alex Solovev,


User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
Old 11-29-2017, 05:04 AM
  tony_sologubov's Avatar 
tony_sologubov tony_sologubov is offline

X-Cart team
Join Date: Jan 2009
Posts: 2,431

Default Re: Show random products from a category


I have found an example of pulling random entities from a repo here:

I went ahead and tried to apply this to product repo and here is the implementation I came up with:

<?php // vim: set ts=4 sw=4 sts=4 et: namespace XLite\Module\XCExample\ModuleDemo\Model\Repo; /** * The "product" model repository */ abstract class Product extends \XLite\Model\Repo\Product implements \XLite\Base\IDecorator { public function getRandomProducts($amount = 7) { return $this->getRandomProductsNativeQuery($amount)->getResult(); } /** * @param int $amount * @return ORM\NativeQuery */ public function getRandomProductsNativeQuery($amount = 7) { # set entity name $table = $this->getClassMetadata() ->getTableName(); # create rsm object $rsm = new \Doctrine\ORM\Query\ResultSetMapping(); $rsm->addEntityResult($this->getEntityName(), 'p'); $rsm->addFieldResult('p', 'product_id', 'product_id'); # make query return $this->getEntityManager()->createNativeQuery(" SELECT p.product_id FROM {$table} p ORDER BY RAND() LIMIT 0, {$amount} ", $rsm); } }

Please, let me know if it works for you.

Found a bug in X-Cart? Post it to our bug tracker!
Know how to make X-Cart better? Suggest an idea!
Reply With Quote

The following 2 users thank tony_sologubov for this useful post:
qualiteam (11-29-2017), voidopolis (08-08-2018)
Old 08-08-2018, 07:48 PM
voidopolis voidopolis is offline

Join Date: Jun 2018
Posts: 8

Default Re: Show random products from a category

Tony (once again - fantastic resources you always provide) So, I'm pretty new to x-cart. I'm learning as fast as I can... I'm clueless in how you go about applying this to the product repo...
X-Cart Business
Reply With Quote
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

All times are GMT -8. The time now is 12:54 PM.


X-Cart forums © 2001-2020