Jump to content

Show random products in sidebar/homepage


Recommended Posts

From among all the products that has the "Include in Featured Products" checkbox checked (which is checked by default when adding a new product), CubeCart tries up to 15 times to randomly select a product that belongs to a category that is enabled and not hidden, is in stock if stock levels are being enforced, and is itself enabled.

There is a Code Snippet that swaps the Best Sellers with the Latest Products. It could easily be adapted to show a set of randomly chosen products from the Featured Products group.

Edited by bsmither
Link to comment
Share on other sites

If you are still interested in showing Featured Products on the Homepage instead of Latest Products, here are two Code Snippets that will do that.

The first is to get a random subset of all (eligible) products with the 'featured' flag set, and send it to the skin template.

The second is to suppress the Featured Product box on the homepage because the featured products are getting listed there anyway.

In admin, Manage Hooks, Code Snippets tab, click the Add Snippet link. When the page refreshes, there will be a form at the bottom. Fill it in as follows:

Enabled: checked
Unique ID: swap_fp_lp_1@cc6114+
Execution Order: 99
Description: Find random subset of featured products.
Trigger: class.cubecart.latest_products
Version: 1.0
Author: https://forums.cubecart.com/topic/58982-show-random-products-in-sidebarhomepage/
PHP Code:
$swapwhere = $GLOBALS['catalogue']->outOfStockWhere(array('I.status' => '1', 'I.featured' => '1'), 'I');
$swapquery = sprintf("SELECT I.* FROM `%1\$sCubeCart_inventory` AS I JOIN `%1\$sCubeCart_category` AS C ON C.cat_id=I.cat_id AND C.`status`=1 AND $swapwhere ORDER BY I.date_added DESC, I.product_id DESC", $GLOBALS['config']->get('config', 'dbprefix'));
if (is_array($featuredProducts = $GLOBALS['db']->query($swapquery, (int)$GLOBALS['config']->get('config', 'catalogue_latest_products_count'))) && ($count = count($featuredProducts)) > 3) {
    foreach ($featuredProducts as $fP) { $preparedFeaturedProducts[$fP['product_id']] = $fP; }
    $randomSelectionOfFeaturedKeys = (array)array_rand($preparedFeaturedProducts, (int)(1+$count/2));
    $latestProducts = array_intersect_key($preparedFeaturedProducts, array_flip($randomSelectionOfFeaturedKeys));
    $tmpLANG = $GLOBALS['smarty']->getTemplateVars('LANG');
    $tmpFP_LANG = $tmpLANG['catalogue']['latest_products'];
    $tmpLP_LANG = $tmpLANG['catalogue']['title_feature'];
    $tmpLANG['catalogue']['title_feature'] = $tmpFP_LANG;
    $tmpLANG['catalogue']['latest_products'] = $tmpLP_LANG;
    $GLOBALS['smarty']->assign('LANG', $tmpLANG);
    $GLOBALS['session']->set('fp_lp_swapped',true);
}

Click Save.

For the second:

Enabled: checked
Unique ID: swap_fp_lp_2@cc6114+
Execution Order: 99
Description: Supresses Latest Product box on homepage
Trigger: class.gui.display_random_product
Version: 1.0
Author: https://forums.cubecart.com/topic/58982-show-random-products-in-sidebarhomepage/
PHP Code:
if ($GLOBALS['smarty']->getTemplateVars('SECTION_NAME') =='home' && $GLOBALS['session']->get('fp_lp_swapped') ) { $product = false; $GLOBALS['session']->delete('fp_lp_swapped'); }

Click Save.

CubeCart will want you to clear its internal cache.

Edited by bsmither
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.

×
×
  • Create New...