Post by ddpneu » Wed Aug 02, 2017 6:59 pm

Hello,
could somebody help me.
I want to show random products in the Specials module.
I found out that it depends on /catalog/controller/extension/module/special.php, but I can´t change it to work.


I guess that this is the part of code which do that:

Code: Select all

		$data['products'] = array();

		$filter_data = array(
			'sort'  => 'pd.name',
			'order' => 'ASC',
			'start' => 0,
			'limit' => $setting['limit']
		);

		$results = $this->model_catalog_product->getProductSpecials($filter_data);
Could somebody help me how to change it to show in the module random from all products?

Thanks.

New member

Posts

Joined
Sat Sep 14, 2013 4:54 am

Post by artcore » Wed Aug 02, 2017 8:12 pm

You could probably do this:
Make sure the highest number is your total products enabled minus the limit.
Always backup first and use a decent code editor like notepad++ or sublimeText (assuming you have no experience with this)

Code: Select all

		$data['products'] = array();

		$filter_data = array(
			'sort'  => 'pd.name',
			'order' => 'ASC',
			'start' => mt_rand(0,500),
			'limit' => $setting['limit']
		);

		$results = $this->model_catalog_product->getProductSpecials($filter_data);

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by ddpneu » Wed Aug 02, 2017 8:28 pm

Thanks. It works, but it isn´t completly random. It takes every time 5 products in the same order as on special page.
And I can´t keep watching the number of Special products cos it is changing very often.

Do you have some other solution which would be universal?

Thanks.

New member

Posts

Joined
Sat Sep 14, 2013 4:54 am

Post by artcore » Wed Aug 02, 2017 10:14 pm

It only changes upon page load if that's what you meant. The bigger the product count, the more randomness you'd experience.
I guess you can play with the randomizer for more randomness :)
Say max products = 1000, limit 20
randomize min and max
rand(rand(0,500),rand(501,980))

or create an array of start numbers to have bigger leaps for sure.

$start = [0,50,100,200,500];
mt_rand($start[rand(0,4)],1000);

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by ddpneu » Thu Aug 03, 2017 3:00 pm

The first solution is still with the problem of everytime changing the number and I can´t do that. Does the second solution solve that?

I don´t know how to make the array from:

Code: Select all

$start = [0,50,100,200,500];
mt_rand($start[rand(0,4)],1000); 
Could you please add to the code:

Code: Select all

$data['products'] = array();

		$filter_data = array(
			'sort'  => 'pd.name',
			'order' => 'ASC',
			'start' => mt_rand(0,500),
			'limit' => $setting['limit']
		);

		$results = $this->model_catalog_product->getProductSpecials($filter_data);
Thanks.

New member

Posts

Joined
Sat Sep 14, 2013 4:54 am

Post by artcore » Thu Aug 03, 2017 3:41 pm

The first solution is still with the problem of everytime changing the number and I can´t do that. Does the second solution solve that?
I don't know what you mean by this. The 'start' key is just telling the database to return results starting at that number from a total set. So you will get different products if you change this. The rand() function does just that, give a number between min and max. But like I said, this will be triggered upon opening the webpage that has a featured module in the layout. So refresh page and you will get a new set of specials.

imho thekrotek is one of those devs that you can buy safely from. He knows his stuff and has good support. No need to fiddle with code yourself:)

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by ddpneu » Thu Aug 03, 2017 4:10 pm

If I understand well.
For example I have 70 products and set as is your example:
rand(rand(0,500),rand(501,980))
The problem is that in almost all time the module doesn´t show up cos it too high.
If I set to right number now (70 products, show 5):
rand(rand(0,30),rand(31,65))
It will works fine untill the number of 70 products will change. The problem is that on our big e-shop and the number is changing very often.

That´s why I asked if there would be other solution without this limitation.

I know that there is plenty of the payed solution. But I don´t need all that features.
That´s why I asked here how to simply change this line.

Thanks.

New member

Posts

Joined
Sat Sep 14, 2013 4:54 am

Post by artcore » Thu Aug 03, 2017 5:29 pm

OK I understand now what you mean.
Add this to the special module controller

Code: Select all

$total_specials = (int)$this->model_catalog_product->getTotalProductSpecials();
Now you can use this as the max in the rand() function;

Code: Select all

$data['products'] = array();

		$filter_data = array(
			'sort'  => 'pd.name',
			'order' => 'ASC',
			'start' => mt_rand(0,$total_specials-$setting['limit']),
			'limit' => $setting['limit']
		);

		$results = $this->model_catalog_product->getProductSpecials($filter_data);

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by Aus Shop » Mon May 07, 2018 11:16 pm

Hi, how to do this for version 3.0.2.0 ?
Thanks

Newbie

Posts

Joined
Mon Apr 09, 2018 1:54 pm
Who is online

Users browsing this forum: thekrotek and 40 guests