![]() |
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 |
Re: Auto populate featured products table
Do I understand this correctly, it selects random product from within the subcategory as featured products?
|
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 |
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! |
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:
|
Re: Auto populate featured products table
Brilliant!! works perfect, with no errors. just what i needed :D/
|
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
|
Re: Auto populate featured products table
Quote:
well even though it didn't give me any errors i still updated the code works 100% thanks again |
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?
|
Re: Auto populate featured products table
It works for all featured products
|
Re: Auto populate featured products table
Amazing, worked right off the bat, and makes life so much easier. Thanks so much for this contribution!
|
Re: Auto populate featured products table
Hi I was getting the error below, I noticed it by chance, as when I select the Add to All Categories button it just quickly flashes across the screen, and disappears almost instantaneously. It seems to be working though, just not sure what the error is though.
Warning: array_walk()[function.array-walk]: The argument should be an array in /home/anythin5/public_html/store/admin/categories.php on line 151. I've included the a few of the lines above and below the error line that I'm getting. HTML Code:
foreach ($new_temp_parent_cats as $a=>$z) { |
Re: Auto populate featured products table
Thanks Steve, this is absolutely great!
I have changed my button in admin to read "Auto Populate" instead of "Add to all categories" - Makes more sense to me :) If I want to add an extra button of some sort next to my "Auto Populate" button that controls this line below how would I go about it? I have changed mine to 8 instead of 4 but would like to be able to control this from my admin side Code:
$pr_main_cat = 4; # products for front page (categoryid 0) Any help would be greatly appreciated |
Re: Auto populate featured products table
I have turned this into a mod but it's for 4.4.x. If you want me to work it out for you for another version email me with details.
http://www.cflsystems.com/public/everyone/auto_featured_products.png |
Re: Auto populate featured products table
hey, would you be able to post it for 4.4.3 or 4.4.x, i have this working but its not working for my sub categories for some reason...
love it tho big time kudos for you. |
Re: Auto populate featured products table
|
Re: Auto populate featured products table
Hey,
thanks allot i appreciate it.. i love it, at least it works on the front page, but when it comes down to the sub catagories i dont know why but i cannot auto populate it, i have to manually add them... heres an error i get from SQL SQL query : INSERT INTO xcart_featured_products (productid, categoryid, product_order, avail) VALUES ('532', '125','0','Y') Error code : 1062 Description : Duplicate entry '532-125' for key 1 Request URI: /admin/categories.php Backtrace: /home/admin/example.com/xcart/include/func/func.db.php:291 /home/admin/example.com/xcart/include/func/func.db.php:191 /home/admin/example.com/xcart/admin/categories.php:215 i have a few of these errors every time i try to populate it, for some reason it says duplicate but its not.. or at least i dont see it because theres no items there.... are you familure with this??.. anyways even if you dont help me out i think you've done a remarkable job in all honesty, saves us all of time.. Mike |
Re: Auto populate featured products table
Where do you see this (version)? It used to do this yes, but I think it's fixed on 4.4.x (I think). The problem there is sometimes the script selects product that already exists as featured product for the category in question and that's not allowed
|
Re: Auto populate featured products table
im on 4.4.3, its wierd i dont have anything seleceted at a featured item, but for catagories only it doesnt populate only for the front page, but empty catagories and it gives me those errors.. i dont get it.. its so werid.. i've tried stripping it down but im not getting anywhere.
|
Re: Auto populate featured products table
Ok so, i figured soething out, the layout of my page goes like this. Catagories>Catagories1 >Catagories 2> Items, So for example
Male > Company > Brand > items under brand The catagories that get filled are the Front Page, and the Company and Brand Sections, but not the Male Section. its empty... which is wierd... the ffront page is fine too but when it goes to the 1st catagory, nothing, but after that its fine.. this is so wierd... |
Re: Auto populate featured products table
It works just fiine. Tested it on multilevel categories on all 4.4.x and no issues. And actually in your example you should see featured products for Male and Company but not for Brand as Brand doesn't have subcategories
|
Re: Auto populate featured products table
sometimes i do sometimes it only gives 1 product to be featured, while i set it to 4 for sub catagories and 4 front.
Could be something that i glitched up i guess.... but when it enables one product to show, i get an email with that SQL Error... the odd thing is i cant disabled the featured items that are being "duplicated" because i dont know which ones they are, is there a way to access the database and disabled all of them manually under a specific table? |
Re: Auto populate featured products table
quick question, which PHP are you using php 5? and is it Mod_sup?
|
Re: Auto populate featured products table
5 yes
|
Re: Auto populate featured products table
Quote:
No more free Now... |
Re: Auto populate featured products table
how do we set it, so that it only selects products that are in stock ??
|
Re: Auto populate featured products table
You can add to the query " AND $sql_tbl[products].avail > 0"
|
Re: Auto populate featured products table
Still works without a hitch in 4.5! Great mod, thanks.
|
Re: Auto populate featured products table
This module is now 4.6.x ready
|
Re: Auto populate featured products table
is there a way this can populate the featured products every x days?
|
Re: Auto populate featured products table
Not right now. It can be custom added though
|
All times are GMT -8. The time now is 06:17 PM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.