Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - Implementing order limits based on custom criterias

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

like image 203
Vitamin Avatar asked Feb 22 '23 09:02

Vitamin


1 Answers

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.

like image 198
Vinai Avatar answered Mar 15 '23 15:03

Vinai