Magento Maximum Allowed Order Amount

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.
25 comments
Is there any way to do the same in Magento 2. When i used the sales_quote_save_before event to restrict the total, the minicart total is showing the wrong total(including the restricted amount). Any way to fix this issue?
Could you please specify full path for magento 2
Do You have any solution for this problem in Magento 2?
Hi nice post thank you so much, but I want restrict the user from purchasing above a limit of $5000 every month,
eg : for the month of august he can purchase only for $5000, please help me to find a solution for that
Great!
But I am wondering about setting minimum order amount, especially for each customer group
Sometimes, we just want to sell a limited number of products for certain customer groups.
I find out an extension that can solve this issues
http://bsscommerce.com/magento-minimum-order-amount-for-customer-group.html
I think this extension can be matched with my thinking.
Limit order amount for different customer groups is exactly what I need. Thank you.
Pretty useful, though it doesn’t work with Magento 1.9.2 right away. I think I need to modify something in your extension, and that I don’t know. Haha!
I dont think this will work in current magento versions 1.7+ or even earlier versions, because there is a beginTransaction() call prior to the dispatching of the sales_quote_save_before event. As there is a transaction level set, so you will end up in a rollback of the transaction cutting yourself the chance to redirect, hence you get an error. Please correct me if i missed some point, but i debugged this deeply.
The only way I found is to reset the transaction level via reflection.
hi, great extension! Is it possible to limit the amount of items per order in cart instead of dealing with the amount of money?
This works great! Thanks so much, just what I needed. Thanks Branko for your release. This seems to be an effective to boost sales of any Magento store. it’s one of the must-have extensions for your eCommerce Website like Magento.
as i am running two store from a same magento backend i want to set the minimum order amount only for on store not for other. as i have the minimum order amount limit in system/configuration/sales/sales
but it will be applicable for both the store. but i want this only for one store. so can you help me
Hi, sorry but I don’t understand how to add this extension to my magento site (ver. 1.8).
I’ve tried to upload with magento admin but it doesn work.
Could you help me?
I’m getting a redirect loop error using your extension in Magento 1.7.0.2.
Somehow this won’t work with our shop (Magento ver. 1.7.0.2) and i can still add more than X items i have set in the admin. Using 1.0.0.0
That’s incredible useful, thanks a lot! But it’s not working on Magento Enterprise. When you are on the /checkout/cart you get this error: This webpage has a redirect loop 🙁
Great work! Do you have a solution for maximum quantity, too? The customer will be allowed to add maximum 2 items to the cart.
Nice work,
I have a question though :
How can I implement this module for order creation in administration?
thanks
This works great! Thanks so much, just what I needed.
I have install this module but the notification message is not showing. The limit is being enforced, but the error is not appearing in the basket.
Amit,
Yes, this module supports multi-websites/stores/views.
Hello. I’m doing a trade website where the same product has different prices depending on the customer groups (trade accounts).
I’d like to set a maximum order amount for each of the trade client (customer group). Can somebody help me please?
Does it support multiple store views? can the limit be X for one single view and Y for the other store view?
thanks!
Amit
Hi Branko.
Nice post.
But I doubt if float comparison will work flawlessly. Floats are never good for comparison. I would rather allow Maximum Order Amount in integer values or use some other way to compare float values.
Thank you so much for this extension! Works like a charm on my current project. Was cracking our heads on how to achieve this.
Appreciate this alot!
Nice post Branko. We are also doing work on same type of work on Magento Technology. Your code will be helpful for us.
Regards,
Imenso Software