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

Random Image or Random Products - Category Specific

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 11-01-2005, 04:07 AM
  TelaFirma's Avatar 
TelaFirma TelaFirma is offline
 

X-Adept
  
Join Date: Nov 2002
Location: North Carolina USA
Posts: 930
 

Default 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
Reply With Quote
  #2  
Old 11-01-2005, 10:42 AM
 
balinor balinor is offline
 

Veteran
  
Join Date: Oct 2003
Location: Connecticut, USA
Posts: 30,253
 

Default

Nice one
__________________
Padraic Ryan
Ryan Design Studio
Professional E-Commerce Development
Reply With Quote
  #3  
Old 11-09-2005, 02:09 PM
 
micromedia micromedia is offline
 

Member
  
Join Date: Feb 2005
Posts: 27
 

Default

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]
__________________
X Cart Gold Ver. 4.1.6
Reply With Quote
  #4  
Old 11-09-2005, 02:44 PM
  TelaFirma's Avatar 
TelaFirma TelaFirma is offline
 

X-Adept
  
Join Date: Nov 2002
Location: North Carolina USA
Posts: 930
 

Default

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.
Reply With Quote
  #5  
Old 11-30-2005, 08:31 AM
 
Sonia Sonia is offline
 

Advanced Member
  
Join Date: Feb 2005
Posts: 72
 

Default

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.

Thanks!
__________________
Sonia

www.vidaville.com
Xcart Gold 4.0.16
platform - Windows
Reply With Quote
  #6  
Old 11-30-2005, 08:43 AM
  TelaFirma's Avatar 
TelaFirma TelaFirma is offline
 

X-Adept
  
Join Date: Nov 2002
Location: North Carolina USA
Posts: 930
 

Default

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.
Reply With Quote
  #7  
Old 12-29-2005, 04:20 PM
  wiper's Avatar 
wiper wiper is offline
 

Member
  
Join Date: Dec 2005
Posts: 15
 

Default

Same error message with smarty and x/y here But where is the mistake, anybody know ?
__________________
Radek Karban - WebDesign - Reklama.cz (not x-cart site)
several x-cart (gold version 4.0.10 - 4.1.3, linux debian) and a huge pack of mods
Reply With Quote
  #8  
Old 01-04-2006, 12:25 PM
  TelaFirma's Avatar 
TelaFirma TelaFirma is offline
 

X-Adept
  
Join Date: Nov 2002
Location: North Carolina USA
Posts: 930
 

Default

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.
Reply With Quote
  #9  
Old 01-09-2006, 07:48 AM
  NightFire's Avatar 
NightFire NightFire is offline
 

X-Adept
  
Join Date: Jul 2004
Location: The Netherlands
Posts: 561
 

Default

The random products are working great.
Thanks for this great mod.
__________________
Best regards,
Niki Buitenrust Hettema

X-Cart Gold 4.1.8 (Live & Loaded)
Royal Bhaktapur: Handmade products from Nepal - Pashmina's, scarfs, shawls, stola's and more...
Reply With Quote
  #10  
Old 01-19-2006, 04:02 AM
  NightFire's Avatar 
NightFire NightFire is offline
 

X-Adept
  
Join Date: Jul 2004
Location: The Netherlands
Posts: 561
 

Default

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.
__________________
Best regards,
Niki Buitenrust Hettema

X-Cart Gold 4.1.8 (Live & Loaded)
Royal Bhaktapur: Handmade products from Nepal - Pashmina's, scarfs, shawls, stola's and more...
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not 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:02 PM.

   

 
X-Cart forums © 2001-2020