4

I'd like to implement a global order limit on certain products. The point of this is that I want to enable backorders on certain products and define several date periods where there are limits to how many of these individual products that may be ordered.

Currently my custom model is loaded with the relevant information for the chosen date period and attached to the product models when they are loaded as $product->setMyModel(...) on these events:

  • catalog_product_load_after
  • catalog_product_collection_load_after
  • sales_quote_item_collection_products_after_load

Accessing my model with data for a specific product is as simple as calling $product->getMyModel(), which I hence will refer to as simply my model.

This is what I want to do:

1. Whenever a product is added to a cart/quote, or placed in an order, I want to do something like this (pseudocode):

// Somehow get $product and $requestedQty (most likely from an event)
$myModel = $product->getMyModel();
if($myModel->applyOrderLimit()) {
    // ($orderedQty + $requestedQty) <= $orderLimit
    if($myModel->isRequestedQtyAvailable($requestedQty)) {
        // Issue an error and prevent the item from being ordered
        return;
    }
    // $orderedQty += $requestedQty
    $myModel->addToQtyOrdered($requestedQty);
}
// Continue Magentos default behaviour

1.1. I suspect that Mage_CatalogInventory_Item::checkQuoteItemQty() should be overriden to capture the $requestedQty here.

2. Update $myModel::ordered_qty whenever an order is cancelled, refunded or such.

I guess the real question is where do I run this code, and is there anything more to implementing such an order limit and keeping track of the qty's than I have realized?

To me, this seem like quite a complex task. Which is why I need assistance from more experienced Magento developers!

Note: I couldnt figure out how to mix numbered lists and code blocks, but I hope its readable enough

Vitamin
  • 1,526
  • 1
  • 13
  • 27
  • Try to develop an observer against sales_quote_save_before, and just through an addError message in the session, and it will role back automatically. – ShaunOReilly Feb 23 '12 at 05:03
  • The tests I've done show that validating the qty by overriding `Mage_CatalogInventory_Item::checkQty()` works just fine (return false if it doesnt suit me). I may be overlooking something though. Anyway, I still don't know when and how it would be appropriate to add to/subtract from my models `ordered_items` - note that I only want to do this under certain conditions, not for example when the admin updates the qty (see the post) – Vitamin Feb 24 '12 at 12:06

1 Answers1

8

You don't have to resort to rewriting the Mage_CatalogInventory_Model_Stock_Item:.checkQty() method in order to achieve your goal.

If you add an event observer to the event sales_quote_item_qty_set_after, your observer will be triggered in addition to the cataloginventory check.

public function salesQuoteItemQtySetAfter(Varien_Event_Observer $observer)
{
    $quoteItem = $observer->getItem();
    $qty = $quoteItem->getQty();
    $myModel = $quoteItem->getProduct()->getMyModel()

    // Your Logic

    // If not salable set error for the quote item
    $quoteItem->addErrorInfo(
        'mymodule', // origin code
        'currently_not_salable', // error code
        'The Error Message'
    );
}

The sales_quote_item_qty_set_after event is also used by the cataloginventory module to call checkQty(), so you can also examine Mage_CatalogInventory_Model_Observer::checkQuoteItemQty() for additional possibilities on what functionality is available.

Vinai
  • 14,162
  • 2
  • 49
  • 69
  • Thanks for your answer. How do you suppose that I keep track of the number or ordered products? I guess I need to take into account refunds/credit memos, and also changed qty in the backend when an order is reordered? – Vitamin Feb 29 '12 at 15:53
  • I assume `$myModel::ordered_qty` has nothing to do with the inventory stock qty of the products? Is it just an additional counter for each product? – Vinai Feb 29 '12 at 17:11
  • It's a counter for each individual product, yes. If a customer tries to order an item that would exceed `$myModel::order_limit`, an error must be issued. `if(($myModel->getOrderedItems() + $requestedQty) > $myModel->getOrderLimit()) { /* error! */ }` – Vitamin Feb 29 '12 at 17:39
  • One more question to clarify: would a `count(*)` on the `sales_flat_order_item` table grouped by `product_id` work, or does it have to be an independent and freely settable value? – Vinai Feb 29 '12 at 18:37
  • The thing is that only products that were ordered under certain conditions are added to `MyModel::ordered_items`, lets call the condition `Mage::getModel('myModel')->loadByProductId(30)->applyOrderLimit()` which would return a boolean. – Vitamin Feb 29 '12 at 22:09
  • Given what I understand so far, I think I would approach this by adding a new table to store the id's and qty of order items matching your conditions. Then a group by product id with a `SUM(qty)` should give you what you want. Hm, maybe it would be good to keep track of the associated order item ids, too, to update the table for creditmemos and such. – Vinai Feb 29 '12 at 23:48