Magento related products disappears with item max qty in cart
-
13-12-2019 - |
Question
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?
Solution
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.