violinman Posted September 25, 2021 Share Posted September 25, 2021 Hello, could someone please suggest how I change the algorithm which displays "Latest Products" on my hope page. My stock is numbered from 01 to around 180. The algorithm favours the higher numbers so even though I have some products in say 01 to 59 that I want to appear in Latest Products the algorithm will always pick the 6 highest numbers! so the lower numbers are never displayed unless I take out the Latest Products tick from the highest numbers! Any suggestions appreciated! Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 25, 2021 Share Posted September 25, 2021 The Latest Products are chosen based on the database column "date_added" in the "CubeCart_inventory" table. The product must also be in stock (if set as such in the store settings), and Status enabled, and be assigned to a category that is also Status enabled. We can create a Code Snippet that chooses a subset number of products based on whatever you want. What would you like to use to select these products, including a random selection? Quote Link to comment Share on other sites More sharing options...
violinman Posted September 26, 2021 Author Share Posted September 26, 2021 Thank you for your offer of help, much appreciated. I have two groups of products: violins which are numbered from 01 - 65 and violin bows which are numbered from 71 - 165. What would be ideal is for the selection to be random but an equal number from each group, so x number products from 01 - 65 and the same number from 71 - 165. I actually display 6 Latest Products at the present time so this would give me 3 from each group. Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 26, 2021 Share Posted September 26, 2021 Are your products distinctly grouped by categories? That is, for example, all the bows are in a category named "Bows" and all the violins are in another category named "Violins"? I will assume so. Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 27, 2021 Share Posted September 27, 2021 (edited) In admin, Manage Hooks, Code Snippets tab, click the Add Snippet link (located just above the list of existing snippets). Below the list of existing snippets is a form. For the $cat_1_id and $cat_2_id variables, you will need to discern the value of the cat_id for the Bows and Violins categories. Enabled: checked Unique ID: [email protected]+ Execution Order: 1 Description: Finds a limited, random selection of products from each category specified for Latest Products. Trigger: class.cubecart.latest_products Version: 1.0 Author: https://forums.cubecart.com/topic/57250-latest-product-list/ PHP Code: <?php $cat_1_id = 1; $cat_1_count = 3; $cat_2_id = 2; $cat_2_count = 3; // Get all eligible products in cat_1 $products_from_cat_1 = $GLOBALS['catalogue']->getCategoryProducts($cat_1_id,false); // Returns list of products keyed by product_id. // Select random elements $random_product_keys_from_cat_1 = array_rand($products_from_cat_1, $cat_1_count); // Get all eligible products in cat_2 $products_from_cat_2 = $GLOBALS['catalogue']->getCategoryProducts($cat_2_id,false); // Returns list of products keyed by product_id. // Select random elements $random_product_keys_from_cat_2 = array_rand($products_from_cat_2, $cat_2_count); // Combine $latestProducts = array_intersect_key($products_from_cat_1, array_flip($random_product_keys_from_cat_1)) + array_intersect_key($products_from_cat_2, array_flip($random_product_keys_from_cat_2)); Save this. Test. Edited September 28, 2021 by bsmither Quote Link to comment Share on other sites More sharing options...
Don129 Posted September 28, 2021 Share Posted September 28, 2021 I have also same issue but can't find solution if anyone know so please reply. Thanks in advance. McDVOICE Quote Link to comment Share on other sites More sharing options...
violinman Posted September 28, 2021 Author Share Posted September 28, 2021 Hi Brian, sorry for the delay in replying. Yes all of the bows are in: Bows, the violins are in Violins but both bows and violins are also in several other categories, for example Fine quality bows, Fine quality violins. Regards, Brian Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 28, 2021 Share Posted September 28, 2021 Ok, good to know about the extra catagories. But is any bow to be found only in "Fine Quality Bows", and is any violin to be found only in "Fine Quality Violins"? The assumption being made in the Code Snippet is that every bow will be in "Bows", and every violin will be in "Violins". We are using these common, all-encompassing categories instead of product_id numbers because we do not want to get ourselves painted into a corner by running out of product_ids for a newly added bow within the 'designated' range of product_ids for bows, etc. Quote Link to comment Share on other sites More sharing options...
violinman Posted September 28, 2021 Author Share Posted September 28, 2021 Every bow is in Bows and every violin is in Violins regardless of any other categories they might be in as well. Many thanks. Quote Link to comment Share on other sites More sharing options...
violinman Posted September 28, 2021 Author Share Posted September 28, 2021 If I go to manage hooks under "Advanced" there is code snippets but no Add Snippet link, there is an "Import Code Snippet" link which expects an XML file. I am using 6.4.2 Many thanks. Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 28, 2021 Share Posted September 28, 2021 (edited) The row of tabs at the top of admin, Manage Hooks include: "Hooks", "Code Snippets", and "Import Code Snippets" Click on the "Code Snippets" tab. Edited September 28, 2021 by bsmither Quote Link to comment Share on other sites More sharing options...
violinman Posted September 28, 2021 Author Share Posted September 28, 2021 I don't actually have a link to Admin, under Settings I have Store Settings but no hooks tab. Under Advanced I have Manage Hooks but it does not appear anywhere else. My apologies, I have just seen the link. in Code Snippets there is a small link in red Add snippet. So do I change the $cat_1_id = 1; to $cat_1_id = Bows; and $cat_2_id = 2; to $cat_2_id = Violins; Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 28, 2021 Share Posted September 28, 2021 Please determine the actual cat_id numerical value for "Bows" and the actual cat_id numerical value for "Violins". You can do this in admin, Categories, in the list of Categories, ID column. Quote Link to comment Share on other sites More sharing options...
violinman Posted September 28, 2021 Author Share Posted September 28, 2021 Ok I have found that. In add code snippet it asks for: Unique ID, Execution Order, Description (which clear enough) trigger and version. Not sure about Unique ID or Trigger. I am an idiot, I just realised that you had kindly included that info at the top of the code! Quote Link to comment Share on other sites More sharing options...
violinman Posted September 28, 2021 Author Share Posted September 28, 2021 Brian that now works a treat, much appreciated, thank you so much, just what I wanted. Brian One last question, the algorithm does not seem to have any relation to date so if I had marked an item Latest Product 6 months ago it will still display that item, would it be possible to limit the date to say 3 months? Quote Link to comment Share on other sites More sharing options...
violinman Posted September 28, 2021 Author Share Posted September 28, 2021 Sorry, one last point, I have disabled it for now as it is also showing items that are out of stock! It may be the way I am running the site, I leave the item available for purchase so that it can be seen even though it is sold as its stock level is 0. Maybe that is what is causing the problem. Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 28, 2021 Share Posted September 28, 2021 Could be either or both of two things: Hide 'Out-of-Stock' Product is not enabled (does not affect logged-in admins), and/or Allow 'Out-of-Stock' Purchases is enabled. Quote Link to comment Share on other sites More sharing options...
violinman Posted September 28, 2021 Author Share Posted September 28, 2021 Ok thanks, it is the Hide 'Out-of-Stock' product is not enabled, perhaps I need to manage the items differently and just get in the habit of removing the Lates Product tick once sold on a regular basis. Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 28, 2021 Share Posted September 28, 2021 But the snippet is not using the Latest Product flag. I suppose we could. Quote Link to comment Share on other sites More sharing options...
violinman Posted September 28, 2021 Author Share Posted September 28, 2021 That would explain the odd results I was getting. Could you set it to use the latest product flag as well? Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 28, 2021 Share Posted September 28, 2021 Be right back! Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 28, 2021 Share Posted September 28, 2021 Bring that code snippet back up for editing. Change the PHP Code to: <?php $cat_1_id = 1; $cat_1_count = 3; $cat_2_id = 2; $cat_2_count = 3; // Get all product_id values in cat_1 $products_from_cat_1 = $GLOBALS['catalogue']->getCategoryProducts($cat_1_id,false); // Returns list of products keyed with product_id. // Filter out any product that does not have the 'latest' flag set. foreach ($products_from_cat_1 as $prod_cat_1_key => $prod_cat_1) { if (!$prod_cat_1['latest']) unset($products_from_cat_1[$prod_cat_1_key]); } // Select minimum of cat_1_count or remaining cat_1_products random elements to latest_products_group_1 $random_product_keys_from_cat_1 = array_rand($products_from_cat_1, min($cat_1_count, count($products_from_cat_1))); // Get all product_id values in cat_2 $products_from_cat_2 = $GLOBALS['catalogue']->getCategoryProducts($cat_2_id,false); // Returns list of products keyed with product_id. // Filter out any product that does not have the 'latest' flag set. foreach ($products_from_cat_2 as $prod_cat_2_key => $prod_cat_2) { if (!$prod_cat_2['latest']) unset($products_from_cat_2[$prod_cat_2_key]); } // Select minimum of cat_2_count or remaining cat_2_products random elements to latest_products_group_2 $random_product_keys_from_cat_2 = array_rand($products_from_cat_2, min($cat_2_count, count($products_from_cat_2))); // Combine latest_products_group_1 and _2 $latestProducts = array_intersect_key($products_from_cat_1, array_flip($random_product_keys_from_cat_1)) + array_intersect_key($products_from_cat_2, array_flip($random_product_keys_from_cat_2)); Save. Test. Quote Link to comment Share on other sites More sharing options...
violinman Posted September 29, 2021 Author Share Posted September 29, 2021 Hi Brian, I applied the updated code, the behaviour has now changed. It now seems static, it has selected the first consecutive three violins that are ticked Latest Product but always the first three. The bows are also static but for some reason only two bows are shown even though several have the Latest Product ticked! Quote Link to comment Share on other sites More sharing options...
bsmither Posted September 29, 2021 Share Posted September 29, 2021 Let's double-check something: I am looking at WP47. It's main category is "Mid price Violins". The category's parent is "Violins". However, looking at the category "Violins", all I see are four sub-categories and no actual products -- which suggests to me that the specified cat_id numbers in the code snippet is for categories that may not have all that many products associated with them. On the other hand, I see that the category "All Violins" does have almost if not all violins associated. Are you using the cat_ids for "All Violins" and "All Bows"? Quote Link to comment Share on other sites More sharing options...
violinman Posted September 29, 2021 Author Share Posted September 29, 2021 That is correct, the root categories for both are "All Violins" and "All Bows" and I have set the $cat_1_id = to the correct numbers, ie 5 and 11. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.