X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Random Image or Random Products - Category Specific (https://forum.x-cart.com/showthread.php?t=17659)

TelaFirma 11-01-2005 04:07 AM

Random Image or Random Products - Category Specific
 
Ok, I have been reading through all the threads concerning random images and it seems that over time there has been a request for that and/or random products - so here is a freebie.

This Mod searches the product database for products that are relevant to the current situation. This means that it is not only category specific but also user or membership specific. It will pick preset number of products in random order. This number is set within Admin under General Settings -> Modules Options. There is also a template that will take the number of random products returned and pick one to display a random product image. This image is also linked to the product page for that product.

INSTALL INSTRUCTONS:

Within Patch/Upgrade section of X-Cart or within SQL Admin run this query:
Code:

INSERT INTO `xcart_modules` ( `moduleid` , `module_name` , `module_descr` , `active` )
VALUES (
'70', 'Random_Products', 'This module displays random products', 'Y'
);
INSERT INTO `xcart_config` ( `name` , `comment` , `value` , `category` , `orderby` , `type` , `defvalue` )
VALUES (
'number_of_randomproducts', 'Number of Random Products to Display', '3', 'Modules', '75', 'text', '3'
);


Next create a new directory under /modules/ named "Random_Products" and a directory under /skin1/modules/ named "Random_Products".

Within /modules/Random_Products create a file named "random.php" with this content:

Code:

<?php
#
# $Id: random.php,v 1.00.0.0 0 TelaFirma $
#
# Random Products
#

if (!defined('XCART_SESSION_START')) { header("Location: ../../"); die("Access denied"); }

if (!is_numeric($config["Modules"]["number_of_randomproducts"]))
        $config["Modules"]["number_of_randomproducts"] = 0;

#
# Get products data for current category and store it in an array
#
$cat = intval($cat);
if ($cat) {
        $category_data = func_query_first("SELECT categoryid_path FROM $sql_tbl[categories] USE INDEX (PRIMARY) WHERE categoryid='$cat'");
        $result = func_query("SELECT categoryid FROM $sql_tbl[categories] USE INDEX (pam) WHERE categoryid_path LIKE '$category_data[categoryid_path]/%' AND avail='Y'");
        $cat_ids = array();
        if (is_array($result)) {
                foreach($result as $k=>$v) {
                        $cat_ids[] = $v["categoryid"];
                }
        }
        else
                $cat_ids[] = $cat;
        $str = " AND $sql_tbl[products_categories].categoryid IN (".implode(",", $cat_ids).")";
}
$order_by = "RAND()";
$search_query = $str;
#
# Search the products
#
$randoms = func_search_products($search_query, @$user_account["membership"], $order_by, $config["Modules"]["number_of_randomproducts"]);

$smarty->assign("r_products",$randoms);

?>


Next create two files under /skin1/modules/Random_Products/ named "random.tpl" and "random_image.tpl" with the contents as:

random.tpl
Code:

{capture name=dialog}
{if $r_products ne ""}
{include file="customer/main/products.tpl" products=$r_products}
{/if}
{/capture}
{include file="dialog.tpl" content=$smarty.capture.dialog extra="width=100%"}


random_image.tpl
Code:

{assign var="rand" value="0"|mt_rand:"`$config.Modules.number_of_randomproducts-1`"}
<a href=product.php?productid={$r_products[$rand].productid}>{include file="product_thumbnail.tpl" productid=$r_products[$rand].productid image_x=$config.Appearance.thumbnail_width product=$r_products[$rand].product tmbn_url=$r_products[$rand].tmbn_url}</a>


Now, edit your home.php file in root directory and add the following lines under the section that loads the Best Sellers module:

Code:

if($active_modules["Random_Products"])
        include $xcart_dir."/modules/Random_Products/random.php";


** New Step **
This is a new step that you will need to do to make the single random image work on most systems (since that code is using a PHP modifier and not a Smarty modifier). If the single random image is working for you already then you do not need to perform this step.

Open the file /Smarty-2.x.x/Smarty.class.php for editing. On or around line 234 you will see the code:
Code:

'MODIFIER_FUNCS'  => array('count')

This need to be change to read
Code:

'MODIFIER_FUNCS'  => array('count', 'mt_rand')
** New Step End **

Now, where ever you want to display random products, just add this code in the template
Code:

{include file="modules/Random_Products/random.tpl"}
and where you want to display a random product image add this code
Code:

{include file="modules/Random_Products/random_image.tpl"}

The template that displays the random products feeds into the template that you are using to display the product listings site wide. So, if you have your store set to display products 3 per row, then the random products will also display that way. The initial settings in Admin are that the module is enabled and the number of random products are 3. You can disable the module at the same location that all other modules are enabled or disabled.

If you find this Mod useful, donations are welcome :D

balinor 11-01-2005 10:42 AM

Nice one :)

micromedia 11-09-2005 02:09 PM

Hi, I get this error. What is it?

Code:

Warning: Smarty error: math ``x/y'' in ``customer/home.tpl'': parameter ``y'' is empty in W:\www\xcart\Smarty-2.6.9\Smarty.class.php on line 1088

Warning: Smarty error: math ``min(maxquantity+minamount, productquantity+1)'' in ``customer/home.tpl'': parameter ``minamount'' is empty in W:\www\xcart\Smarty-2.6.9\Smarty.class.php on line 1088

[/code]

TelaFirma 11-09-2005 02:44 PM

OK - well - I guess I need to rewrite that query (or the template that is used to display the products). It seems that the function "func_search_products" does not load up the minimum and low-stock ammounts (among other things).

Those errors are part of the math functions in the product templates and the buy-now button template.

Sonia 11-30-2005 08:31 AM

This was exactly what I wanted...but it doesn't appear to be working for me either, although I don't get the error that micromedia gets. I added the referencing code:
Code:

{include file="modules/Random_Products/random.tpl"}

to skin1/customer/main/product.tpl, but all that shows up is a box with a yellow border - here's a sample page http://www.vidaville.com/store/product.php?productid=250&cat=1&page=1. You'll see the empty box near the bottom of the page below the area called "You may also like...".

What I want to be able to do is only show products from certain categories within that area. Is that the goal of this mod? I thought it was, but I could be wrong. :wink:

Thanks!

TelaFirma 11-30-2005 08:43 AM

Actually the goal was to display within a category listing not necessarily on the product page. This is a consideration though. I will see if I can change that to make it work that way.

wiper 12-29-2005 04:20 PM

Same error message with smarty and x/y here:( But where is the mistake, anybody know ?

TelaFirma 01-04-2006 12:25 PM

I have made a small change to the random.php file that will clear up some issues that some people are having - specifically the issue of not getting an error message, but not getting any images either.

I have also added an extra step that you may need to do if the single random image is not working for you.

NightFire 01-09-2006 07:48 AM

The random products are working great.
Thanks for this great mod.

NightFire 01-19-2006 04:02 AM

Got a question about the random images. Is it also possible to include the product title above the random image? And maybe the product price. That would be very nice.

moneysaver67 01-20-2006 08:35 AM

NightFire 50% Answer
 
Quote:

Originally Posted by NightFire
Got a question about the random images. Is it also possible to include the product title above the random image? And maybe the product price. That would be very nice.


As for the title, that's an easy one...

Go to random_image.tpl and add the following line above (or within) the a href= tag...


Just guessing, but I assumed the rand was returning outside the bounds of $config.Modules.number_of_randomproducts

Sure enough, this occurred as I had expected.
$config.Modules.number_of_randomproducts was set to 3, and occassionaly $rand resulted as 3, but arrays are an option base 0, so 3 is outside of the boundaries. To modify my above temporary fix, I account for this, and subtract 1 if necessary, resulting in the following:

Code:

{assign var="rand" value="0"|mt_rand:$config.Modules.number_of_randomproducts}
{if $rand eq $config.Modules.number_of_randomproducts}
        {math assign="rand" equation="x-1" x=$rand}
{/if}
{$r_products[$rand].product}

<a href=product.php?productid={$r_products[$rand].productid}>{include file="product_thumbnail.tpl" productid=$r_products[$rand].productid image_x=$config.Appearance.thumbnail_width product=$r_products[$rand].product tmbn_url=$r_products[$rand].tmbn_url}</a>


TelaFirma 01-20-2006 10:10 AM

You are correct about the array and sometimes it will generate a random number outside the array limits. I corrected my post. All that was necessary was to change the assign to :

Code:

{assign var="rand" value="0"|mt_rand:"`$config.Modules.number_of_randomproducts-1`"}

Good catch.

NightFire 01-20-2006 10:42 AM

It's working great. Thanks for the fixes.
Another question. The random image is displaying the thumbnail size which is set under the general settings. But is it also possible to change this image size in the random_image.tpl so that it has a custom size? The products wich are displayed in the categories will still have their original size then.


Edit: Found a bug in the random image script. I placed the random image script on the homepage. But when you click on the product you'll be redirected to the product page. But the place where the random image script, is displaying no image availlable and it isn't displaying a product title. And it redirects me to this when I click on it:
www.shopurl.com/shop/image.php?productid=
Hope someone find a solution for this.

moneysaver67 01-23-2006 07:09 AM

Quote:

Originally Posted by NightFire
But the place where the random image script, is displaying no image availlable and it isn't displaying a product title. And it redirects me to this when I click on it:
www.shopurl.com/shop/image.php?productid=
Hope someone find a solution for this.


NightFire,

This is the same error that we were speaking about above (Prev 2 Posts) Please implement the fix to correct this.

sandersdesign 01-25-2006 12:19 PM

hi, I seem to be having problems with implementing this mod. please visit http://www.digi-quick.com/error_message.php?access_denied&id=33


home.tpl
Code:

<?php
if ($active_modules["Random_Products"])
        include $xcart_dir."/modules/Random_Products/random.php";
?>
{include file="modules/Random_Products/random.tpl"}
{include file="modules/Random_Products/random_image.tpl"}



random.php
Code:

<?php
#
# $Id: random.php,v 1.00.0.0 0 TelaFirma $
#
# Random Products
#

if (!defined('XCART_SESSION_START')) { header("Location: ../../"); die("Access denied"); }

if (!is_numeric($config["Modules"]["number_of_randomproducts"]))
  $config["Modules"]["number_of_randomproducts"] = 0;

#
# Get products data for current category and store it in an array
#
$cat = intval($cat);
if ($cat) {
  $category_data = func_query_first("SELECT categoryid_path FROM $sql_tbl[categories] USE INDEX (PRIMARY) WHERE categoryid='$cat'");
  $result = func_query("SELECT categoryid FROM $sql_tbl[categories] USE INDEX (pam) WHERE categoryid_path LIKE '$category_data[categoryid_path]/%' AND avail='Y'");
  $cat_ids = array();
  if (is_array($result)) {
      foreach($result as $k=>$v) {
        $cat_ids[] = $v["categoryid"];
      }
  }
  else
      $cat_ids[] = $cat;
  $str = " AND $sql_tbl[products_categories].categoryid IN (".implode(",", $cat_ids).")";
}
$order_by = "RAND()";
$search_query = $str;
#
# Search the products
#
$randoms = func_search_products($search_query, @$user_account["membership"], $order_by, $config["Modules"]["number_of_randomproducts"]);

$smarty->assign("r_products",$randoms);

?>



random.tpl

Code:

{capture name=dialog}
{if $r_products ne ""}
{include file="customer/main/products.tpl" products=$r_products}
{/if}
{/capture}
{include file="dialog.tpl" content=$smarty.capture.dialog extra="width=100%"}


random_image.tpl
Code:

{assign var="rand" value="0"|mt_rand:"`$config.Modules.number_of_randomproducts-1`"}
{$r_products[$rand].product}
 
<a href=product.php?productid={$r_products[$rand].productid}>
{include file="product_thumbnail.tpl" productid=$r_products[$rand].productid image_x=$config.Appearance.thumbnail_width product=$r_products[$rand].product tmbn_url=$r_products[$rand].tmbn_url}</a>


balinor 01-25-2006 12:22 PM

Look at the install instructions again....you have some php referenced in home.tpl when it should be in home.php.

sandersdesign 01-25-2006 01:48 PM

thanks balinor, I've got it to work now.

only thing is that it's not really what I was looking for. I was trying to figure out a way adapt the 'Featured Products' tools so that it only shows random products from specified categories. Has such a mod been developed.

Thanks for any help.

balinor 01-25-2006 01:51 PM

That is exatly what this does?

moneysaver67 01-28-2006 09:56 AM

What about disabled categories?
 
TelaFirma,

This is a sweet Mod, Tela! Wanted to point out that another thing that I also noticed is that the random image returned sometimes returns images from DISABLED categores. This is not ideal, at least for our shop.

I'm sure I could correct this, but reviewing func_search_products within func.php didn't lead me to any 'smoking gun' right off the bat, anyhow...

Don't know if you were aware of this...

moneysaver67 01-28-2006 09:58 AM

Quote:

Originally Posted by sandersdesign
I was trying to figure out a way adapt the 'Featured Products' tools so that it only shows random products from specified categories.


Isn't it just a matter of placing this on one of your category pages?

Code:

#
# Get products data for current category and store it in an array
#
$cat = intval($cat);

if ($cat) {
       
  $category_data = func_query_first("SELECT categoryid_path FROM $sql_tbl[categories] USE INDEX (PRIMARY) WHERE categoryid='$cat'");
  $result = func_query("SELECT categoryid FROM $sql_tbl[categories] USE INDEX (pam) WHERE categoryid_path LIKE '$category_data[categoryid_path]/%' AND avail='Y'");

...


balinor 02-08-2006 04:44 PM

Anyone got a hack to this that will display a totally random product, no matter what page the user is on? Currently I've got this set up in a spot that shows on every page, and when it goes to non-product/category pages it shows up blank....

moneysaver67 02-09-2006 08:08 PM

Isn't it just a matter of placing this on one of your category pages?

Code:

#
# Get products data for current category and store it in an array
#
$cat = intval($cat);

if ($cat) {
       
  $category_data = func_query_first("SELECT categoryid_path FROM $sql_tbl[categories] USE INDEX (PRIMARY) WHERE categoryid='$cat'");
  $result = func_query("SELECT categoryid FROM $sql_tbl[categories] USE INDEX (pam) WHERE categoryid_path LIKE '$category_data[categoryid_path]/%' AND avail='Y'");

...



Simply modify the SQL to query the table, but not limiting to $cat ....

then, ORDER BY RAND() and LIMIT 1 ...you should be set. LIMIT 3 if you use the random_products.tpl

tomcoleman 02-19-2006 11:48 AM

I see that thi smod display RANDOM IMG's and Products

I just want mine to display Random Featured Products

Do i need to do all of this mod or is there a shorter way of doing it??

TheWrongGrape 07-23-2006 11:59 PM

How to show products from only one category?
 
I've been able to get this to work great (thanks!) but was wondering if there was something I could modify so that it is:

1. Independent of the category page it is currently being displayed on and instead

2. Shows only random products from a category I specify, for example, I just want it to show products in "Category D" which has a categoryid of 8, no matter what page it's on.

I assume I need to edit random.php and the SQL query, but I don't know how. Any help would be appreciated.

:) Thank you

TheWrongGrape 07-24-2006 12:05 AM

I figured it out!
 
Sorry, after a few more trials and errors, I figured it out!

In random.php, I simply changed

Code:

# Get products data for current category and store it in an array
#
$cat = intval($cat);


to

Code:

# Get products data for current category and store it in an array
#
$cat = 8;


Where the number 8 above is the categoryid for the category who's products I want to display. Hope this helps someone!

robin 12-19-2006 06:23 AM

Re: Random Image or Random Products - Category Specific
 
This mod is working great I was just wondering if someone could add some functionality I cannot figure out? I would like to have on the home page 4 different specified categorys with 3 random featured products a piece. I would like these to be displayed in a row instead of a column. Is any of this possible?

ian@rs.nl 01-18-2007 02:16 AM

Re: Random Image or Random Products - Category Specific
 
Great mod - nearly works

I'm now getting an SQL error when I click on my categories..

"INVALID SQL: 1176 : Key 'pam' doesn't exist in table 'xcart_categories'
SQL QUERY FAILURE:SELECT categoryid FROM xcart_categories USE INDEX (pam) WHERE categoryid_path LIKE '12/%' AND avail='Y'"

Apart from this - the mod is fantastic.

Thanks,
Ian

ian@rs.nl 01-18-2007 03:38 AM

Re: Random Image or Random Products - Category Specific
 
---don't panic - i've fixed it by switching the INDEX from (pam) to (pa)


Great mod

ian@rs.nl 03-21-2007 06:38 AM

Re: Random Image or Random Products - Category Specific
 
If anyone's still keeping an eye on this thread was there ever a solution for displaying the product price? Product name was solved but..

dub713 06-22-2007 02:59 PM

Re: Random Image or Random Products - Category Specific
 
well the mod sounds great but i'll pose one question before i run off and start implementing something that may not work as needed.

i'd ideally like to be able to display 3-4 random products in a row, with images, with links to the product page obviously, but i only want to do this on certain pages where the layout allows for this. mainly the main page, and some other page that don't contain much information other some text.

i know you can call this from where ever you want, but i know there was mention of one random image, and that is was specific to certain categories. the pages i'd want this on have no category, or membership affliation.

is this doable?

thanks,

nfc5382 06-22-2007 06:51 PM

Re: Random Image or Random Products - Category Specific
 
is there a mod to randomize the featured products? also to only randomize it every 24 hours or so? I hacked my featured products to sort by RAND but each page refresh causes the featured products to randomize. I keep it only b/c it keeps the main page changing.... however, i'd like a more user friendly randomizer for the featured products

inmotionmedia 09-21-2007 01:12 AM

Re: Random Image or Random Products - Category Specific
 
Hey you say in the forum that you hacked the random function for the featured products.
How and where is that possible? I want to show random products on the featured products list limited by an number. It may change every time that's no problem


tnx

shopccp 10-25-2007 11:51 AM

Re: Random Image or Random Products - Category Specific
 
There's a problem with this mod. The quantity field is not filled with a number, by default. The user is unable to put the item in the shopping cart from the buy now button.


Any ideas to fix???

TelaFirma 10-25-2007 05:05 PM

Re: Random Image or Random Products - Category Specific
 
I see by your sig that you are using X-Cart version 4.1.x - if you look at the date of the original post, it was in 2005. This was intended for version 4.0.x - not 4.1.x....

Quote:

Originally Posted by shopccp
There's a problem with this mod. The quantity field is not filled with a number, by default. The user is unable to put the item in the shopping cart from the buy now button.


Any ideas to fix???


shopccp 10-26-2007 02:25 AM

Re: Random Image or Random Products - Category Specific
 
OK. Sorry. So this mod is unsupported in 4.1.x

It seems to be a simple fix. Since it works in your ver 4.0.x. How hard is it to give the quantity field a number to add the item to the cart from the buy now button. Just exactly how the x-cart team did for the featured products - it should be the same for the random products.

TelaFirma 10-26-2007 03:00 AM

Re: Random Image or Random Products - Category Specific
 
Well - I agree but I have not had the time to look through this code again to make it work for 4.1.x.

In giving it a quick look the first thing that you need to look at is the product array that is being built and make sure that the "min_amount" has a value. The buy_now template uses this to determine what to do with qty. If "min_amount" is empty then you will need to look at the function used to do the search.

Quote:

Originally Posted by shopccp
OK. Sorry. So this mod is unsupported in 4.1.x

It seems to be a simple fix. Since it works in your ver 4.0.x. How hard is it to give the quantity field a number to add the item to the cart from the buy now button. Just exactly how the x-cart team did for the featured products - it should be the same for the random products.


shopccp 10-26-2007 09:23 AM

Re: Random Image or Random Products - Category Specific
 
The value is already assigned at 1. The featured products uses the same buy now template as the random products.

august 10-26-2007 12:18 PM

Re: Random Image or Random Products - Category Specific
 
http://forum.x-cart.com/showthread.php?t=34157

mpcommercial 10-07-2008 01:20 PM

Re: Random Image or Random Products - Category Specific
 
Hello:

Has anyone gotten this great mod to work in version 4.1x?

Makey2u 05-25-2011 03:37 PM

Re: Random Image or Random Products - Category Specific
 
anyone got this for 4.4.x??? working
oh man i would drool...


All times are GMT -8. The time now is 07:08 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.