0

I have written a customized code to add different price of product on product page:

Code in: app/code/local/custom/price/etc/config.xml

<?xml version="1.0"?>
<config>
  <global>
    <models>
        <price>
             <class>custom_price_Model</class>
        </price>
    </models>
    <events>
      <checkout_cart_product_add_after>
          <observers>
               <custom_price_observer>
               <class>price/observer</class>
               <method>modifyPrice</method>
               </custom_price_observer>
          </observers>
      </checkout_cart_product_add_after>
    </events>
  </global>
</config> 

Code in : app/code/local/custom/price/Model/Observer.php

class custom_price_Model_Observer
{
    public function modifyPrice(Varien_Event_Observer $obs)
    {
        // Get the quote item
        $item = $obs->getQuoteItem();
        // Ensure we have the parent item, if it has one
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        // Load the custom price
        $price = "30";
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
    }

Still its not working. Kindly Help.

2 Answers2

1

You have to do following way to save your custom price.

class custom_price_Model_Observer
{
     public function modifyPrice(Varien_Event_Observer $obs)
     {
         // Get the quote item
         $item = $obs->getQuoteItem();
         // Ensure we have the parent item, if it has one
         $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        // Load the custom price
        $price = "30";
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
        $item>save();
    }
}
Hardik Visa
  • 323
  • 6
  • 23
0

below is the code i used for apply discount for products after added to cart,i have applied 50% discount for products added to cart and it works for me.

function modifyPrice(Varien_Event_Observer $observer){
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();
    $product_id=$quote_item->getProductId();
    $product_price = Mage::getModel('catalog/product')
                        ->load($product_id)
                        ->getPrice();         
    if($product_price != 0){           
        $percentDiscount = 0.50;
        $specialPrice = $product_price - ($product_price * $percentDiscount);  
        $quote_item->setOriginalCustomPrice($specialPrice);        
    }
}
Suman r
  • 75
  • 1
  • 9