How to change price for buy all stock for a product in magento

By | April 22, 2017
Spread the love

Default magento  have option to change product price based on qty by tire pricing rules in admin panel. But if any admin wants to give another discount on take all stock then he needs to create module to achieve it.

You can also create product attribute like minimum qty should be exist to apply take all price logic. So based on min qty and discounted price will apply when user will add product to cart. If user will change qty after adding all stock the price will change to regular product price.

Lets explain how you can do it. Create a test module with file. Techgroup_OrderUpdate.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Techgroup_OrderUpdate>
            <active>true</active>
            <codePool>local</codePool>
        </Techgroup_OrderUpdate>
    </modules>
</config>

Create a config.xml file in module  etc folder.

<config>
    <modules>
        <Techgroup_OrderUpdate>
            <version>1.0.0.0</version>
        </Techgroup_OrderUpdate>
    </modules>
    <global>
        <models>
            <techgroup_orderupdate>
                <class>Techgroup_OrderUpdate_Model</class>
            </techgroup_orderupdate>
        </models>    
        <sales>
           <quote>
		<item>
		   <product_attributes>
		      <last_stock_min_qty />
		       <last_stock_price />
		       <shipping_price />
	            </product_attributes>
	         </item>
	     </quote>
        </sales>         
        <events>			    
            <sales_quote_save_after>
                <observers>
                   <techgroup_orderupdate_update_price>
                      <type>singleton</type>
                      <class>techgroup_orderupdate/observer</class>
                      <method>changePrice</method>
                   </techgroup_orderupdate_update_price>
               </observers>
            </sales_quote_save_after>
	</events>  	
    </global>
</config>

Create a model class Observer.php

<?php

class Techgroup_OrderUpdate_Model_Observer
{
	public function getNewPrice($quote_item)
	{
		$product  = $quote_item->getProduct();
		$stock    = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
		
		$_product = Mage::getModel("catalog/product")->load($product->getId());
		if(!$_product){
			return false;
		}
		
		if(!$_product->getLastStockMinQty() || !$_product->getLastStockPrice()){
			return false;
		}
		
		$newprice = $_product->getPrice();
		$prices   = $_product->getTierPrice();
		
		foreach($prices as $group => $price){
			list($name,$qty) = explode("-", $group);
			
			if($quote_item->getQty() >= $qty){
				$newprice = $price['price'];
			}
		}
		
		$allStockPrice = false;
		
		if($_product->getLastStockMinQty() && $_product->getLastStockPrice()){
			if((int)$stock->getQty() == (int)$quote_item->getQty() && $quote_item->getQty() >= $_product->getLastStockMinQty())
			{
				$newprice = $_product->getLastStockPrice();
				
				$allStockPrice = true;
			}
		}
		
		return $newprice;
	}
	
	public function changePrice(Varien_Event_Observer $observer)
	{
		if(Mage::app()->getStore()->isAdmin()){
			return false;
		}
		
		$storeId = Mage::app()->getStore()->getStoreId();
		
		$event  = $observer->getEvent();
		$quote  = $event->getQuote();
		$items  = $quote->getAllVisibleItems();
		
		foreach($items as $item)
		{
			$new_price  = $this->getNewPrice($item);
			
			if($new_price){
				$item->setOriginalCustomPrice($new_price);
				$item->getProduct()->setIsSuperMode(true);
			}
		}
	}
}

 

Leave a Reply

Your email address will not be published.

fourteen − one =