Question

I have a situation where the product price should change based on the number of items on inventory. Example, if the stock has > 10 item then the product price should be some special price like 100$. If the stock is < 10 then the price should show the original price of the product.

I have created a custom module with a custom attribute(minQtyApplicableForSpecialPrice) for minimum quantity applicable for the special price. Now I have to change all the places like the product, cart and checkout pages where the price should show the special price based on minQtyApplicableForSpecialPrice value.

Any help is appreciated. Thanks in advance.

Was it helpful?

Solution

Finally found my own solution by adding an Observer event.

sales_order_place_after can change product price after placing the order if the stock is less than minQtyApplicableForSpecialPrice.

public function salesOrderAfterSave(Varien_Event_Observer $observer)
{

    $order = $observer->getEvent()->getOrder();
    $cartItems = $order->getQuote()->getAllVisibleItems();
    foreach ($cartItems as $item) {
       $productId = $item->getProductId();
       $product = Mage::getModel('catalog/product')->load($productId);
       $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
       $stockQuantity=$stock->getQty();

       $attribute_code='minimum_qty_for_special_price';
       $minimumQuantity = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
       $date = Mage::getModel('core/date')->date('Y-m-d h:i:s');
       $previousDate =  Mage::getModel('core/date')->date('Y-m-d h:i:s', strtotime($date." -1 days"));

       if (($stockQuantity - $minimumQuantity) < 0){
            $product->setSpecialToDate($previousDate);
            Mage::app()->setCurrentStore(0);
            $product->save();
        } 

    }
    return $this;
}

What I did is, by changing special price date to the previous date. so that, special price will not show up if the stock is less than minimum quantity required. Hence the actual price will be displayed for products that have stock less than minimum quantity.

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