Auto Populate Cross Sell with Products from Current Category Magento
April 16th, 2011 Posted by Nick Cron Public 26 comments on “Auto Populate Cross Sell with Products from Current Category Magento”eCommerce Data is clear – you need to have cross sell items for every product in your catalog to help conversion. The usual process consists of manually associating items in the Magento Admin interface. This process works fine if you have hundreds or even a few thousand items – OR – you have this data in back end systems.
What happens if you don’t have the data and you have 15,000+ items in your catalog?
- Hire someone to associate all products?
- Randomly apply products in your data feed?
- Develop custom code?
All of the above items are true for my Medical Supplies business – Medical Delivered so I needed a solution. I decided to auto assign cross sell products on the front end on product page render.
In action: http://www.medicaldelivered.com/medline-k1-excel-wheelchair.html
Rules:
- The item must be “visible”
- The item must reside in the same category as the current item
- Don’t display the item from the current product detail page
Results:
- All products automatically have cross sells displayed
- No need to assign products on the back end
- BAD: Less control over items displayed
This solution won’t work for everyone, if you are an advanced retailer you won’t want to lose this control but if you just want cross sells and don’t have the time/resources to define manually you can try the code below.
Path: app/design/frontend/default/<template>/catalog/product/list/upsell.phtml
(you may need to create this path – make sure to replace <template> with your template name)
<?php /** * Magento * * NOTICE OF LICENSE * * This source file is subject to the Academic Free License (AFL 3.0) * that is bundled with this package in the file LICENSE_AFL.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/afl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@magentocommerce.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade Magento to newer * versions in the future. If you wish to customize Magento for your * needs please refer to http://www.magentocommerce.com for more information. * * @category design_default * @package Mage * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * @author Nick Cron * @authorweb www.njcmedia.com */ ?> <?php $_related = $this->getProduct(); // get the parent id to skip $_parentid = $_related->getId(); if ($_related) { // get collection of categories this product is associated with $categories =$_related->getCategoryCollection() ->setPage(1, 1) ->load(); // if the product is associated with any category if ($categories->count()) foreach ($categories as $_category) { $cur_category = Mage::getModel('catalog/category')->load($_category->getId()); ?> <div> <div><h4><?php echo $this->__('You may also be interested in the following product(s)') ?></h4></div> <table cellspacing="0" id="upsell-product-table"> <tr> <?php $visibility = array( Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG ); $products = Mage::getResourceModel('catalog/product_collection') ->addCategoryFilter($_category) ->addAttributeToFilter('visibility', $visibility) ->addAttributeToSelect('small_image'); $products->getSelect()->limit(5); // count the number of displayed products $_i=0; foreach ( $products as $productModel ) { $_related = Mage::getModel('catalog/product')->load($productModel->getId()); $_realtedid = $_related->getId(); // prevents displaying the same product your are already on if ($_realtedid != $_parentid && $_i<4): ?> <td> <p><a href="<?php echo $_related->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_related, 'small_image')->resize(125) ?>" width="125" height="125" alt="<?php echo $this->htmlEscape($_related->getName()) ?>" /></a></p> <h5><a href="<?php echo $_related->getProductUrl() ?>"><?php echo $this->htmlEscape($_related->getName()) ?></a></h5> <?php echo $this->getPriceHtml($_related, true) ?> <?php echo $this->getReviewsSummaryHtml($_related) ?> </td> <?php // increment displayed products $_i++; endif; } ?> </tr> <?php } } ?> </table> <script type="text/javascript">decorateTable('upsell-product-table')</script> </div>
Room for improvement:
- Display items from the back end if exist
- Display only items with images
- ..?
I hope this helps someone. If you have any suggestions for improvement or code upgrades/fixes please let me know in the comments.