Jump to content

Latest Product List


violinman
 Share

Recommended Posts

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!

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by bsmither
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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"?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...