Magento - Custom criteria based implementation restrictions

I would like to implement a global order limit for some products. The fact is that I want to include backups for certain products and define several periods of dates when there are restrictions on the number of these individual products that can be ordered.

Currently, my user model is loaded with relevant information for a selected period of time and attached to product models when they are loaded as $product->setMyModel(...) into 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 will therefore simply call my model.

This is what I want to do:

1. Whenever a product is added to the cart / quote or placed in 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 overridden to capture $requestedQty here.

2. Update $myModel::ordered_qty when the order is canceled, returned, or such.

I guess the real question is where do I run this code, and is there anything else to implement such an order limitation and tracking quantity than I understood?

For me this seems like a pretty daunting task. That's why I need help from more experienced Magento developers!

Note. I couldn't figure out how to mix numbered lists and blocks of code, but I hope its readable enough

+4
source share
1 answer

You do not need to resort to rewriting the Mage_CatalogInventory_Model_Stock_Item:.checkQty() method to achieve your goal.

If you add an event observer to the sales_quote_item_qty_set_after event, your observer will be launched in addition to checking the catalog.

 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 sales_quote_item_qty_set_after also used by the cataloginventory module to call checkQty() , so you can also examine Mage_CatalogInventory_Model_Observer::checkQuoteItemQty() for more functionality on the functionality available.

+8
source

Source: https://habr.com/ru/post/1397806/


All Articles