Posts tagged "Cross Sell"

Auto Populate Cross Sell with Products from Current Category Magento

April 16th, 2011 Posted by 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:


  1. The item must be “visible”
  2. The item must reside in the same category as the current item
  3. Don’t display the item from the current product detail page


  1. All products automatically have cross sells displayed
  2. No need to assign products on the back end
  3. 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)

* Magento
* 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:
* 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 so we can send you a copy immediately.
* 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 for more information.
* @category   design_default
* @package    Mage
* @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (
* @license  Academic Free License (AFL 3.0)
* @author        Nick Cron
* @authorweb
$_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)

// 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><h4><?php echo $this->__('You may also be interested in the following product(s)') ?></h4></div>
<table cellspacing="0" id="upsell-product-table">



$visibility = array(

$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('visibility', $visibility)


// count the number of displayed products

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):


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

// increment displayed products


<?php }
<script type="text/javascript">decorateTable('upsell-product-table')</script>

Room for improvement:

  1. Display items from the back end if exist
  2. Display only items with images
  3. ..?

I hope this helps someone.  If you have any suggestions for improvement or code upgrades/fixes please let me know in the comments.