2

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

2
  • $item = $observer->getEvent()->getQuoteItem(); $item = ( $item->getParentItem() ? $item->getParentItem() : $item ); $_product = Mage::getModel('catalog/product')->load($item->getProductId()); $finalPrice = $Customprice + $_product->getPrice(); $item->setCustomPrice($finalPrice); $item->setOriginalCustomPrice($finalPrice); $item->getProduct()->setIsSuperMode(true); $item->save(); After that when customer as guest logged in and customer add to cart any of the product at that time display the error like: "Cannot add the item to shopping cart" How to fix this error?
    – user60743
    Commented Mar 8, 2018 at 11:50
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Jarnail S
    Commented Mar 8, 2018 at 12:14

1 Answer 1

2

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); 

}
25
  • This event can also insert the custom amount into the field CardWalletAmount added in sales_flat_quote_item table?
    – Anto S
    Commented Jun 21, 2015 at 10:15
  • Where is your CardWalletAmount column? In which table? Commented Jun 21, 2015 at 10:16
  • its on sales_flat_quote_item, user can enter any amount on product detail page only, once user click on add to cart, then the amount should be updated. I think above event trigger on showing product detail page itself
    – Anto S
    Commented Jun 21, 2015 at 10:21
  • I tried that too. Field card_wallet_amount get updated, but still in shopping cart only the product price is showing not the custom amount sum up with product amount
    – Anto S
    Commented Jun 21, 2015 at 10:33
  • Your event is triggering when the product detail page is loaded, not the instant at adding the product into cart
    – Anto S
    Commented Jun 21, 2015 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.