PHP Magento Tips

How to Limit Maximum no. of Items in Cart in Magento

Rate this posting:
{[['']]}

In Magento Default you will find many options to limit no. of cart items.

1.) In System >> Configuration >> Sales >> Sales you will find Minimum Order Amount.

And enter your Minimum Amount (Subtotal after Discount).

So, if your subtotal is less than minimum amount then you can not go with "Proceed Checkout" Step.

2.) In System >> Configuration >> Catalog >> Inventory >> Product Stock Options

You will find "t ".

So if you enter More qty for any item which is greater than Mention in above field then it will throw an error.

In the same field, you will find "Minimum Qty Allowed in Shopping Cart"

Here you can define Minimum Qty for different customer group.

So if you enter less qty than Mentioned in above field then it will automatically increase the qty in your cart.

Now, What you will do if you want to Limit Certain no. of items to be allowed in your shopping cart. (Suppose i want to allow only 5 different items to be added in cart.)

For that let's make a simple module for this. Here we create sales_quote_save_before frontend event to Limit no. of items in cart.

Step 1: Create Module initialization file.

Create file in app/etc/modules/Mycompany_Cartmaxproduct.xml and add below code in it.

<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_Cartmaxproduct>
            <active>true</active>
            <codePool>local</codePool>
        </Mycompany_Cartmaxproduct>
    </modules>
</config>

Step 2: Create Module config and system.xml files.

a:) Create file in app/code/local/Mycompany/Cartmaxproduct/etc/config.xml and add below code in it.

<?xml version="1.0" encoding="UTF-8"?>

<config>

    <modules>

        <Mycompany_Cartmaxproduct>

            <version>1.0.0.0</version>

        </Mycompany_Cartmaxproduct>

    </modules>

    <global>

        <models>

            <mycompany_cartmaxproduct>

                <class>Mycompany_Cartmaxproduct_Model</class>

            </mycompany_cartmaxproduct>

        </models>

        <helpers>

            <mycompany_cartmaxproduct>

                <class>Mycompany_Cartmaxproduct_Helper</class>

            </mycompany_cartmaxproduct>

        </helpers>

    </global>

    <frontend>

        <events>

            <sales_quote_save_before>

                <observers>

                    <mycompany_cartmaxproduct_enforceCartOrderLimit>

                        <class>Mycompany_Cartmaxproduct_Model_Observer</class>

                        <method>enforceCartOrderLimit</method>

                    </mycompany_cartmaxproduct_enforceCartOrderLimit>

                </observers>

            </sales_quote_save_before>

        </events>

    </frontend>

    <default>

        <sales>

            <mycompany_cartmaxproduct>

                <active>1</active>

                <cart_max_amount>10</cart_max_amount>

                <cart_max_amount_msg><![CDATA[The Maximum allowed number of items in your cart is:]]></cart_max_amount_msg>

            </mycompany_cartmaxproduct>

        </sales>

    </default>

</config>

b:) Create file in app/code/local/Mycompany/Cartmaxproduct/etc/system.xml and add below code in it.

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <sections>
        <sales translate="label" module="sales">
            <groups>
                <mycompany_cartmaxproduct>
                    <label>Maximum Number of Items In Cart</label>
                    <sort_order>51</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <active translate="label">
                            <label>Enable</label>
                            <sort_order>5</sort_order>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </active>
                        <cart_max_amount>
                            <label>Max Number of Items</label>
                            <comment><![CDATA[What is the max amount of items you want to allow in your cart.]]></comment>
                            <frontend_type>text</frontend_type>
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </cart_max_amount>
                        <cart_max_amount_msg>
                            <label>Max number of items error value</label>
                            <comment><![CDATA[Message to be displayed to customer when "Max Number of Items" condition limit is triggered.  Defaults to "The Maximum allowed number of items in your cart is"]]></comment>
                            <frontend_type>text</frontend_type>
                            <sort_order>30</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </cart_max_amount_msg>
                    </fields>
                </mycompany_cartmaxproduct>
            </groups>
        </sales>
    </sections>
</config>

Step 3: Create Helper File. Create file in app/code/local/Mycompany/Cartmaxproduct/Helper/Data.php and add below code in it.

<?php
class Mycompany_Cartmaxproduct_Helper_Data extends Mage_Core_Helper_Abstract
{
    const XML_PATH_ACTIVE                           = 'sales/mycompany_cartmaxproduct/active';
    const XML_PATH_CART_MAX_AMOUNT                  = 'sales/mycompany_cartmaxproduct/cart_max_amount';
    const XML_PATH_CART_MAX_AMOUNT_MSG              = 'sales/mycompany_cartmaxproduct/cart_max_amount_msg';

    public function isModuleEnabled($moduleName = null)
    {
        if ((int)Mage::getStoreConfig(self::XML_PATH_ACTIVE, Mage::app()->getStore()) != 1) {
            return false;
        }

        return parent::isModuleEnabled($moduleName);
    }

    public function getCartMaxAmount($store = null)
    {
        return (int)Mage::getStoreConfig(self::XML_PATH_CART_MAX_AMOUNT, $store);
    }

    public function getCartMaxAmountMsg($store = null)
    {
        return Mage::getStoreConfig(self::XML_PATH_CART_MAX_AMOUNT_MSG, $store);
    }
}

Step 4: Create Observer and Create Method in it. Create file in app/code/local/Mycompany/Cartmaxproduct/Model/Observer.php and add below code in it.

<?php
class Mycompany_Cartmaxproduct_Model_Observer
{
    private $_helper;

    public function __construct()
    {
        $this->_helper = Mage::helper('mycompany_cartmaxproduct');
    }

    /**
     * Cannot have more than x number of products in the cart
     */
    public function enforceCartOrderLimit($observer)
    {
         if (!$this->_helper->isModuleEnabled()) {
            return;
        }
        $cart = Mage::getModel('checkout/cart')->getQuote();
        if ($cart->getItemsCount() > (float)$this->_helper->getCartMaxAmount()) {

            $maxCount = (float)$this->_helper->getCartMaxAmount();
            $fullWarning = $this->_helper->getCartMaxAmountMsg().' '.$maxCount;
            Mage::getSingleton('checkout/session')->addError($fullWarning);
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
            Mage::app()->getResponse()->sendResponse();
            exit;
        }
    }
}
Tag : Magento
1 Comments On "How to Limit Maximum no. of Items in Cart in Magento"

The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..
ecommerce website development company | ECommerce Development Company in Bangalore|Ecommerce Website Design and Development Company in Bangalore | Magento Development Company

Back To Top