Magento 1.9 – Custom Price for a Product

event-observermagento-1.9priceshopping-cart

I am developing a store in which you can buy a card at fixed price and can add any amount as per users wish.

So when product added to cart product price should be calculated in such a way that product price+amount choosen by customer. in addition to this other concepts are as same as magento default function like shipping mod charges, delivery charges etc..

To do this I am creating an observer event checkout_cart_product_add_after.

I created a module and calling checkout_cart_product_add_after event.

Below in my module config file

<config>
.
.
.
<global>
<events>
        <checkout_cart_product_add_after>
            <observers>
                <ucs_catalogorder_checkout_cart_product_add_after>
                    <type>model</type>
                    <class>Ucs_CatalogOrder_Model_Observer</class>
                    <method>checkoutCartProductAddAfter</method>
                </ucs_catalogorder_checkout_cart_product_add_after>
            </observers>
        </checkout_cart_product_add_after>

    </events>
</global>
</config>

and below is the Observer.php

<?php
class Ucs_CatalogOrder_Model_Observer
{
    public function checkoutCartProductAddAfter($observer) {
        $item = $observer->getEvent()->getQuoteItem();

        $walletAmount = Mage::app()->getRequest()->getPost('card_wallet_amount');


        $item->setCustomPrice($walletAmount);
        $item->setOriginalCustomPrice($walletAmount);
        $item->setCardWalletAmount($walletAmount);
        //$item->save();
        $item->getProduct()->setIsSuperMode(true);

        return $this;
    }

I have a custom field card_wallet_amount in my sales_flat_quote_item table, this field, custom_price and original_custom_price fields are getting updated with custom value, but total price is not updating based on the changes.

please suggest what goes wrong here.

As per my question database level, custom prices updating but on cart page, it is still showing only original product price.

enter image description here

enter image description here
enter image description here

Best Answer

I doubt it's not getting called in config or bundle item. Please try this code in your observer.php.

<?php
class Ucs_CatalogOrder_Model_Observer
{
    public function checkoutCartProductAddAfter($observer) {
    $item = $observer->getEvent()->getQuoteItem();
    if ($item->getParentItem()) {
       $item = $item->getParentItem();
    }
    $walletAmount = Mage::app()->getRequest()->getPost('card_wallet_amount');

    //not sure why calling $item->getOriginalPrice() not getting product's price
    //getting price from catalog model
    $productPrice = Mage::getModel('catalog/product')->load($item->getProductId())->getFinalPrice();

    //adding custom with original price
    $finalPrice = (int)$walletAmount + (int)$productPrice;

    if($finalPrice > 0){
        $item->setCustomPrice($finalPrice);
        $item->setOriginalCustomPrice($finalPrice);
        $item->setCardWalletAmount($finalPrice);
        $item->getProduct()->setIsSuperMode(true);
    }
        //Mage::getSingleton('core/session')->addSuccess($finalPrice); 

}