Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to change the Product Price at the time of adding it to cart in Magento?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 621
    Comment on it

    In Magento sometimes we are required to apply our own custom price on the products while adding to the cart. 
    To do so we can work upon the event checkout_cart_product_add_after.

     

    Lets see how we can do it:

    1. Create a module with namespace as Custom and module as Price.
    2. Then Create a file config.xml in etc folder in our module.
    3. Afterwards create a file Observer.php in Model folder.Now, in our config.xml file at the path Custom/Price/etc/config.xml write the below code in it :

     

    <?xml version="1.0"?>
    <config>
      <modules>
        <Custom_Price>
          <version>0.1.1</version>
        </Custom_Price>
      </modules>
      <frontend>
        <events>
          <checkout_cart_product_add_after>
            <observers>
              <discount>
                <class>custom_price/observer</class>
                <method>setPrice</method>
              </discount>
            </observers>
          </checkout_cart_product_add_after> 
        </events>
       </frontend> 
    </config>
    

     

     
    In the above code we have defined our module with the version 0.1.1 and 
    then in global we have called our Observer at the event checkout_cart_product_add_after
     
    Now in our Observer at the path Custom/Price/Model/Observer.php write the below code:

     

    <?php
    
    class Custom_Price_Model_Observer{
     
     public function setPrice($observer) {
     
     $new_price = 20;
        $item = $observer->getQuoteItem();
        $item->setCustomPrice($new_price);
        $item->setOriginalCustomPrice($new_price);
        $item->getProduct()->setIsSuperMode(true); 
     }   
    }

    In the above code we are defining the product price first with $new_price variable, which is then used to set the Price value of the products at the time of adding it to cart.
    In this way we can change the value of $new_price and apply our own custom product price.  

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: