Recently I have been working on a project that had its own set of small fraud prevention features. One such feature was maximum allowed order amount. While it may sound strange at first; why would someone want to limit the maximum amount of order?! Given the nature of the product that shop was selling (cannot disclose that information) that feature request seemed pretty reasonable. If you look at the Magento admin configuration area, you will see that Magento offers the opposite feature called Minimum Order Amount. You can find it under System > Configuration > Sales > Sales > Minimum Order Amount.
Minimum Order Amount functions slightly different than our Maximum Allowed Order Amount. The main difference is that with Maximum Allowed Order Amount you are not suppose to add anything new to the cart itself if it exceeds the maximum limit. So how do we do that. Answer is simple, event/observer system. All we need to do is to find the right event in Magento and hook an observer on it. After careful tracing, few try and fail, and a tip from my coworker Matej, the most logical event for implementing Maximum Allowed Order Amount seemed to be sales_quote_save_before. Additionally, it makes sense to implement the Maximum Allowed Order Amount functionality only on the frontend area, since we do not want to limit the admin created orders. With that in mind, we only needed to add the following to our extension config.xml file:
<config>
<frontend>
<events>
<sales_quote_save_before>
<observers>
<inchoo_maxorderamount_enforceSingleOrderLimit>
<class>inchoo_maxorderamount/observer</class>
<method>enforceSingleOrderLimit</method>
</inchoo_maxorderamount_enforceSingleOrderLimit>
</observers>
</sales_quote_save_before>
</events>
</frontend>
</config>
And the following logic into our Observer.php model class file:
class Inchoo_MaxOrderAmount_Model_Observer
{
private $_helper;
public function __construct()
{
$this->_helper = Mage::helper('inchoo_maxorderamount');
}
/**
* No single order can be placed over the amount of X
*/
public function enforceSingleOrderLimit($observer)
{
if (!$this->_helper->isModuleEnabled()) {
return;
}
$quote = $observer->getEvent()->getQuote();
if ((float)$quote->getGrandTotal() > (float)$this->_helper->getSingleOrderTopAmount()) {
$formattedPrice = Mage::helper('core')->currency($this->_helper->getSingleOrderTopAmount(), true, false);
Mage::getSingleton('checkout/session')->addError(
$this->_helper->__($this->_helper->getSingleOrderTopAmountMsg(), $formattedPrice));
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
Mage::app()->getResponse()->sendResponse();
exit;
}
}
}
The real success with this approach lies in the single “Mage::getSingleton(‘checkout/session’)->addError() …” line of code. Triggering this line of code here, within sales_quote_save_before event, makes Magento refuse to update (add new items) the shopping cart if the amount of new products exceeds the Maximum Allowed Order Amount. More or less thats it.
You can download Inchoo_MaxOrderAmount Magento extension from my GitHub account. It adds a nice little configuration area for this extension under System > Configuration > Sales > Sales > Maximum Order Amount. On top of just limiting the maximum allowed order amount, there is also some extra functionality regarding the admin email notifications in case certain orders pass certain amount values. As I said, this extension is inspired by simple fraud prevention functionalities on a project we have been working on recently.
Hope you find it useful, if not for practical application then maybe for educational reasons.
Cheers.