Instead of rewriting a controller (please oh please don't do that), rather, rewrite the addProduct
method to account for the limit:
class YourCompany_YourModule_Model_Cart extends Mage_Checkout_Model_Cart
{
public function addProduct($productInfo, $requestInfo=null){
if($this->getItemsCount()>1){
Mage::throwException(Mage::helper('checkout')->__('Cannot add item - cart quantity would exceed checkout the limit of %s per person.', 1));
}
parent::addProduct($productInfo, $requestInfo);
}
}
If you want to get fancy, replace the 1
above with Mage::getStoreConfig('checkout/options/max_cart_qty)
and set the following your module's config.xml:
<default>
<checkout>
<options>
<max_cart_qty>1</max_cart_qty>
</options>
</checkout>
</default>
That value is now controlled via the XML value. If you want to get really, really fancy, add this to the system.xml of your new module:
<config>
<sections>
<checkout>
<groups>
<options>
<fields>
<max_cart_qty translate="label">
<label>Maximum Quantity Allowed in Cart (total qty)</label>
<frontend_type>text</frontend_type>
<sort_order>100</sort_order>
<show_in_default>1</show_in_default>
</max_cart_qty>
</fields>
</options>
</groups>
</checkout>
</sections>
</config>
Remember that you need to set a <depends>Mage_Checkout</depends>
to your module in order to piggyback on its' predefined system configuration.
checkout_cart_product_add_before
there, e.g. markshust.com/2012/08/27/…