3

In Magento we have set "Display Out of Stock Products" => "Yes"

On product pages, we have a "Related products" section (related.phml => if($this->getItems()->getSize()):)

However when Maximum Qty Allowed in Shopping Cart has been reached, the product disappears from the related products (as well as upsells).

Why is this and how can I display these products anyway?

3
+50

The products should not appear in related or upsells if they are already in the cart. The qty that is in the cart is not important in this case.
Here is how it works in the Related.php block.

Mage_Catalog_Block_Product_List_Related::_prepareData looks like this:

protected function _prepareData()
{
    $product = Mage::registry('product');
    /* @var $product Mage_Catalog_Model_Product */

    $this->_itemCollection = $product->getRelatedProductCollection()
        ->addAttributeToSelect('required_options')
        ->setPositionOrder()
        ->addStoreFilter()
    ;

    if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
        Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
            Mage::getSingleton('checkout/session')->getQuoteId()
        );
        $this->_addProductAttributesAndPrices($this->_itemCollection);
    }
    //Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);

    $this->_itemCollection->load();

    foreach ($this->_itemCollection as $product) {
        $product->setDoNotUseCategoryId(true);
    }

    return $this;
}

Notice the lines

Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
    Mage::getSingleton('checkout/session')->getQuoteId()
);

this is a call to Mage_Checkout_Model_Resource_Cart::addExcludeProductFilter that looks like this

public function addExcludeProductFilter($collection, $quoteId)
{
    $adapter = $this->_getReadAdapter();
    $exclusionSelect = $adapter->select()
        ->from($this->getTable('sales/quote_item'), array('product_id'))
        ->where('quote_id = ?', $quoteId);
    $condition = $adapter->prepareSqlCondition('e.entity_id', array('nin' => $exclusionSelect));
    $collection->getSelect()->where($condition);
    return $this;
}

and what it does is to get the ids of the product already in the cart and adds a where entity_id NOT IN (...) statement to the select for the related products.

The same thing happens to Mage_Catalog_Block_Product_List_Upsell::_prepareData.
So, unless you modified this behavior, the max qty allowed in cart is not important.

  • Thanks! How did I miss this :-( I was looking at the lines below addSaleableFilterToCollection and addVisibleInCatalogFilterToCollection. I must say that I find it weird that this is default behaviour and not manageable from the CMS. A person might add 1 product to cart, then add a second and find out that the first product also goes with the second and need another one. – Richard Oct 12 '15 at 9:40
  • I'm a little confused. This does not explain why products disappear when out of stock, just that they disappear once in the basket. Is that what you were after? – Robert Egginton Oct 12 '15 at 9:53
  • I was confused, I though it was when they were out of stock only. But turns out the disappear once in basket already. So this was my solution – Richard Oct 23 '15 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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