Magento – Allow only one product in cart (extension)

The events catalog_product_type_prepare_full_options
and catalog_product_type_prepare_lite_options

1. create config /app/code/community/Nikunj/Cart/etc/config.xml

<?xml version=”1.0″?>
<config>
<modules>
<Nikunj_Cart>
<version>0.1.0</version>
</Nikunj_Cart>
</modules>
<frontend>
<events>
<catalog_product_type_prepare_full_options>
<observers>
<nikunj_cart_catalog_product_type_prepare>
<class>Nikunj_Cart_Model_Observer</class>
<method>catalogProductTypePrepare</method>
</nikunj_cart_catalog_product_type_prepare>
</observers>
</catalog_product_type_prepare_full_options>
</events>
</frontend>
</config>

2.Create observer /app/code/community/Nikunj/Cart/Model/Observer.php

<?php

class Nikunj_Cart_Model_Observer extends Varien_Object
{
public function catalogProductTypePrepare($observer)
{
$quote = Mage::getSingleton(‘checkout/session’)->getQuote();
if($quote->getItemsCount()>=1){
Mage::throwException(‘You can only buy one product at a time.’);
}
}
}

Advertisements