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

Auto populate featured products table

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 02-25-2010, 06:14 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Auto populate featured products table

I got a little tired of manually changing featured products for categories picking products one by one, waiting on page to load, open the browse products window again and again and again.... So here is something that auto populates them with a click of a button.
This code is tested and working on 4.2.1 (no other versions tested). Use at your own risk. The code populates with products featured_products table for category 0 (that is front page) and any category with subcategories (if category doesn't have subcategories there will be no featured products selected for that category); products are selected from all active products assign to category and its subcategories. Keep in mind this was written to serve my needs so it may not work for you the way it is but feel free to adjust it any way you want.

BACKUP STORE/DATABASE FIRST

1. Open admin/categories.php and find

} elseif ($mode == "add" && intval($newproductid) > 0) {
#
# Add new featured product
#
$newavail = (!empty($newavail) ? "Y" : "N");
if ($neworder == "") {
$maxorder = func_query_first_cell("SELECT MAX(product_order) FROM $sql_tbl[featured_products] WHERE categoryid='$f_cat'");
$neworder = $maxorder + 10;
}
if (func_query_first("SELECT productid FROM $sql_tbl[products] WHERE productid='$newproductid'")) {
db_query("REPLACE INTO $sql_tbl[featured_products] (productid, product_order, avail, categoryid) VALUES ('$newproductid','$neworder','$newavail', '$f_cat')");
$top_message["content"] = func_get_langvar_by_name("msg_adm_featproducts_upd ");
}
}

and below that add

#
# Add featured products for all categories - CFL Systems
#
elseif ($mode == "add_all") {
function flatten_array($value, $key, &$array) {
if (!is_array($value))
array_push($array,$value);
else
array_walk($value, 'flatten_array', &$array);
}

db_query("DELETE FROM $sql_tbl[featured_products]"); #empty featured products table

$pr_main_cat = 4; # products for front page (categoryid 0)
$pr_sub_cats = 3; # products for each category with subcategory
$newavail = "Y";
$neworder = 0;

$temp_products = func_query("SELECT $sql_tbl[products].productid FROM $sql_tbl[products] JOIN $sql_tbl[products_categories] ON $sql_tbl[products].productid = $sql_tbl[products_categories].productid WHERE forsale='Y' AND $sql_tbl[products_categories].main = 'Y' ORDER BY RAND() LIMIT $pr_main_cat");
$new_temp_products = array();
array_walk($temp_products, 'flatten_array', &$new_temp_products);
foreach ($new_temp_products as $k=>$v) {
db_query("INSERT INTO $sql_tbl[featured_products] (productid, product_order, avail, categoryid) VALUES ('$v','$neworder','$newavail', '0')");
$neworder = $neworder + 10;
}
unset($temp_products);
unset($new_temp_products);
$temp_parent_cats = func_query("SELECT DISTINCT categoryid FROM $sql_tbl[categories_subcount] WHERE subcategory_count!='0'");
$new_temp_parent_cats = array();
array_walk($temp_parent_cats, 'flatten_array', &$new_temp_parent_cats);
foreach ($new_temp_parent_cats as $a=>$z) {
$temp_cats = func_query("SELECT categoryid FROM $sql_tbl[categories] WHERE parentid='$z'");
$new_temp_cats = array();
array_walk($temp_cats, 'flatten_array', &$new_temp_cats);
$temp_products = func_query("SELECT $sql_tbl[products].productid FROM $sql_tbl[products] LEFT JOIN $sql_tbl[products_categories] ON $sql_tbl[products].productid = $sql_tbl[products_categories].productid WHERE $sql_tbl[products].forsale='Y' AND $sql_tbl[products_categories].categoryid IN ('$z','".implode("','", $new_temp_cats)."') AND $sql_tbl[products_categories].main = 'Y' ORDER BY RAND() LIMIT $pr_sub_cats");
$new_temp_products = array();
array_walk($temp_products, 'flatten_array', &$new_temp_products);
$neworder = 0;
foreach ($new_temp_products as $c=>$d) {
db_query("INSERT INTO $sql_tbl[featured_products] (productid, product_order, avail, categoryid) VALUES ('$d','$neworder','$newavail', '$z')");
$neworder = $neworder + 10;
}
unset($temp_products);
unset($new_temp_products);
}
}

2. Open skin1/admin/main/featured_products.tpl and find

<tr>
<td colspan="4" class="SubmitBox">
<input type="submit" value="{$lng.lbl_add_new|strip_tags:false|escape}" onclick="javascript: document.featuredproductsform.mode.value = 'add'; document.featuredproductsform.submit();"/>
</td>
</tr>

and add below

<tr>
<td colspan="4" class="SubmitBox">
<input type="submit" value="Add to all categories" onclick="javascript: document.featuredproductsform.mode.value = 'add_all'; document.featuredproductsform.submit();"/>
</td>
</tr>

3. Run cleanup.php


Go to admin/categories, at the bottom just below the featured products there should be "add to all categories" button
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote

The following user thanks cflsystems for this useful post:
Dan.roh (03-18-2011)
  #2  
Old 02-25-2010, 07:59 AM
  JWait's Avatar 
JWait JWait is offline
 

X-Man
  
Join Date: Nov 2005
Location: California
Posts: 2,440
 

Default Re: Auto populate featured products table

Do I understand this correctly, it selects random product from within the subcategory as featured products?
__________________
Two Separate X-Cart Stores
Version 4.4.4 Gold - X-AOM - Vivid Dreams Aquamarine (modified) - Linux
Mods - Newest Products - View All -, and a few others. Numerous upgrades from 4.0.x series.
Integrated with Stone Edge Order Manager + POS

Version 4.1.12 Gold (fresh install) - X-AOM - Linux
Mods - XCSEO free
Reply With Quote
  #3  
Old 02-25-2010, 09:10 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Auto populate featured products table

It will select randomly "x" products from category and its subcategories. Lets say you have
Cat A
Subcat B
Subcat C

It will select randomly form all the products in Cat A + Subcat B + Subcat C
Subcat B and C will not have featured products - they will list the products assign to them, regular xcart behaviour

If you have
Cat A
Subcat B
Subcat C
Subcat D
It will list featured products for Cat A and Subcat C
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #4  
Old 08-10-2010, 10:58 AM
 
hhiker hhiker is offline
 

eXpert
  
Join Date: May 2007
Posts: 231
 

Default Re: Auto populate featured products table

Is there a 4.3 version and does this set a limit to how long it will search for products on the home page? We have over 1,000 products and rapidly climbing so I don't want to slow down the site.

Thx!
__________________
-
-
Versions: 4.1.10 and 4.3.0 (see post for which cart)

"Until man duplicates a blade of grass, nature can laugh at his so-called scientific knowledge." - Thomas Edison

"Never doubt that a small group of thoughtful, committed citizens can change the world; indeed, it is the only thing that ever has." - Margaret Mead (1901-197 quoted in John M. RIchardson, ed. Making it Happen, 1982

"Water is the best of all things." - Pindar (c. 522 BC - c. 438 BC), Olympian Odes
Reply With Quote
  #5  
Old 08-10-2010, 04:41 PM
 
hhiker hhiker is offline
 

eXpert
  
Join Date: May 2007
Posts: 231
 

Default Re: Auto populate featured products table

Well, stuck it in 4.3 and it works ok but... I get this error on the top of the page.

Quote:
Deprecated: Call-time pass-by-reference has been deprecated in C:\wamp\www\site\admin\categories.php on line 124

Deprecated: Call-time pass-by-reference has been deprecated in C:\wamp\www\site\admin\categories.php on line 136

Deprecated: Call-time pass-by-reference has been deprecated in C:\wamp\www\site\admin\categories.php on line 145

Deprecated: Call-time pass-by-reference has been deprecated in C:\wamp\www\site\admin\categories.php on line 149

Deprecated: Call-time pass-by-reference has been deprecated in C:\wamp\www\site\admin\categories.php on line 152
__________________
-
-
Versions: 4.1.10 and 4.3.0 (see post for which cart)

"Until man duplicates a blade of grass, nature can laugh at his so-called scientific knowledge." - Thomas Edison

"Never doubt that a small group of thoughtful, committed citizens can change the world; indeed, it is the only thing that ever has." - Margaret Mead (1901-197 quoted in John M. RIchardson, ed. Making it Happen, 1982

"Water is the best of all things." - Pindar (c. 522 BC - c. 438 BC), Olympian Odes
Reply With Quote
  #6  
Old 10-22-2010, 01:07 AM
  Phoenixone's Avatar 
Phoenixone Phoenixone is offline
 

Advanced Member
  
Join Date: Feb 2010
Location: Portugal
Posts: 38
 

Default Re: Auto populate featured products table

Brilliant!! works perfect, with no errors. just what i needed
__________________
PHP 5.2.11
MYSQL 5.0.45

WESH UK hosting

--- Add-ons -----
X-RMA
X-AOM
X-FancyCategories
X-Feature comparison

BCSE Request a Quote
BCSE Customer Testimonials
BCSE Advanced ratings and reviews
BCSE Customer reward points
BCSE Reward points refferal
BCSE Customer polls

Cart Labs - Tabs
Reply With Quote
  #7  
Old 10-22-2010, 03:10 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Auto populate featured products table

I have recently discovered a small bug if you wanna call it that. It will throw sql error if a product exists in more then one category and it is selected twice. I fixed the code in the first post
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #8  
Old 10-22-2010, 04:12 AM
  Phoenixone's Avatar 
Phoenixone Phoenixone is offline
 

Advanced Member
  
Join Date: Feb 2010
Location: Portugal
Posts: 38
 

Default Re: Auto populate featured products table

Quote:
Originally Posted by cflsystems
I have recently discovered a small bug if you wanna call it that. It will throw sql error if a product exists in more then one category and it is selected twice. I fixed the code in the first post

well even though it didn't give me any errors i still updated the code works 100%

thanks again
__________________
PHP 5.2.11
MYSQL 5.0.45

WESH UK hosting

--- Add-ons -----
X-RMA
X-AOM
X-FancyCategories
X-Feature comparison

BCSE Request a Quote
BCSE Customer Testimonials
BCSE Advanced ratings and reviews
BCSE Customer reward points
BCSE Reward points refferal
BCSE Customer polls

Cart Labs - Tabs
Reply With Quote
  #9  
Old 10-22-2010, 12:52 PM
  fmoses's Avatar 
fmoses fmoses is offline
Banned
 

Senior Member
  
Join Date: Apr 2008
Posts: 155
 

Default Re: Auto populate featured products table

Anyone try it out on v 4.1.9? From my understanding it sounds like it will work for Home page featured products as well?
Reply With Quote
  #10  
Old 10-22-2010, 06:56 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Auto populate featured products table

It works for all featured products
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote

The following user thanks cflsystems for this useful post:
fmoses (10-22-2010)
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 07:40 PM.

   

 
X-Cart forums © 2001-2020