I have this block of code which displays product from a certain category in the cart for last minute buys.

$categoryid = 1103;
$category = new Mage_Catalog_Model_Category();
$collection = $category->getProductCollection();
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));?>

<div class="upsell_products">
<?php $i = 0; ?>
<?php foreach ($collection as $_product){ ?>
<?php $product = Mage::getModel('catalog/product')->load($_product->getId());?>
<div class="product">
<a href="<?php echo $_product->getProductUrl() ?>">
<div class="product_img_cart">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>" alt="<?php echo $_product->getName(); ?>" /></a>
<a href="<?php echo $_product->getProductUrl(); ?>">
<p class="upsell_pro_name"><?php echo $_product->getName(); ?></p>
<p class="upsell_pro_price">
<?php echo Mage::helper('core')->formatPrice($_product->getPrice());?></a></p>
<div class="add_cart_btn">
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($product)?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php if($i++ == 4) break; ?>
<?php }?>

I want to add in a check so that if one of the "upsell" products are already in the cart then it is not offered to the customer.

If this doesn't make sense or you need me to explain more please just ask

Thank you

2 Answers 2


See app\code\core\Mage\Checkout\Block\Cart\Crosssell.php:

protected function _getCartProductIds()
    $ids = $this->getData('_cart_product_ids');
    if (is_null($ids)) {
        $ids = array();
        foreach ($this->getQuote()->getAllItems() as $item) {
            if ($product = $item->getProduct()) {
                $ids[] = $product->getId();
        $this->setData('_cart_product_ids', $ids);
    return $ids;

So for your collection you can add

$collection->addFieldToFilter('entity_id', array('nin'=>$idsInCart));
  • ok so this function gets the Product ID that are in the cart? But how would i use that to make sure none of them products appear in my upsell section ?
    – Adam Allen
    Commented Oct 14, 2014 at 11:46
  • Yes, then you can add NOT IN condition for your collection. I've updated the answer
    – Amasty
    Commented Oct 14, 2014 at 11:47
  • I have added that to my collection but how would i wrap a check around my products? Sorry am stilling learning magento and PHP code
    – Adam Allen
    Commented Oct 15, 2014 at 15:08

You can use the method Mage_Sales_Model_Quote::getItemByProduct() which also checks for parent items and custom options, so in your case it returns true if and only if the product is added to the cart on its own with no configuration.

Put these lines at the beginning of your foreach loop:

if (Mage::getSingleton('checkout/session')->getQuote()->getItemByProduct($_product)) {

If you don't want the check to be so picky and just see if this product is somehow in the cart, check if the product id is found in the quote items:

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$itemsProductIds = array_map(function($item) { return $item->getProductId(); }, $items);
if (in_array($_product->getId(), $itemsProductIds) {

Unrelated note: loading each item of a collection again in a loop hurts your performance since it results in many unnecessary database queries. You should prepare the collection in a way that it already loads all attributes and joins all tables that you need.

  • I will try your method after lunch :) Thank you
    – Adam Allen
    Commented Oct 14, 2014 at 11:46
  • 2
    @AdamAllen how was lunch? :) Commented Dec 29, 2015 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.