Question

I am implementing a plugin for a client that will use the 'Max Sale Quantity' value of a Magento Stock Item on a product as a means of setting an upper-bound on total customer purchases.

Because I need to check this condition in multiple events, controller actions, I cannot contain it to merely an Observer where I could create a local protected function that checks this condition from my Resource Model.

In my requirements I will be limiting the cart additions. But I find myself in a dilemma: from the controller addAction I need to rely on throw - however from sales_quote_merge_before it seems that I could effectively unset the offending products and post a message to the customer/session, though, I could throw and catch, respectively as with the controller action instead.

My questions are:

  • Is this bad practice to effectively rely on throw from the module helper?
  • Is this, rather, a model method that I need to invoke?
  • What is the best design strategy that allows me to keep my module DRY?

Edit:

As of EE 1.12 I see only 4 instances of Helper/Data.php files that throw - 3 of them are Api2/Oauth. Mage/Core does it once as well, but this seems to be the only other instance. Various 3rd parties look like they take different approaches here.

Any feedback is welcome.

Was it helpful?

Solution

Saying to the customer, "we are sorry, you can't purchase so much" is a exception. The expected case is, there is enough and you put the products in the cart. And one more word: There are try and catch blocks inside of magento, for example in \Mage_Checkout_CartController::addAction:

// [...]
} catch (Mage_Core_Exception $e) {
        if ($this->_getSession()->getUseNotice(true)) {
            $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
// [...]

Let me say it this way: They are there to be used :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top