I have created a custom module and created an observer method for "sales_quote_collect_totals_before" event. In this method, I check some of the quote data. Based on the current checkout data, I'd like to apply MSRP prices on products that are placed in the cart. Or switch products back to normal prices, if the criteria are not satisfied.

How can I do that?

Here's a sample of the code:

public function changeQuotePrices(Varien_Event_Observer $observer){
    if (Mage::app()->getRequest()->getModuleName() == 'onepage'){
        $event = $observer->getEvent();
        $quote = $event->getQuote();
        $quoteShippingAddress = $quote->getShippingAddress();
        if (!is_null($quoteShippingAddress->getId())){
            $quoteShippingAddress->collectShippingRates()->save();
            $quoteShippingMethod = $quoteShippingAddress->getShippingMethod();
            if($quoteShippingMethod && $quoteShippingMethod == "payonpickup_payonpickup"){ 
                $cart = Mage::helper('checkout/cart')->getCart()->getQuote()->getAllItems();    
                foreach($cart as $item) {
                    $product = Mage::getModel('catalog/product')->load($item->getProductId()); 
                    $_msrpPrice = Mage::helper('core')->currency($product->getMsrp(),true,true);
                    if($_msrpPrice > 0){
                        /* apply msrp product prices */
                    }
                }
            }else{ 
                /* apply normal product prices */
            }

        }

    }
}
  • had you tried my answer below? – Chirag Rajput May 16 at 5:00
up vote 5 down vote accepted
+50

You can try below code:

public function changeQuotePrices(Varien_Event_Observer $observer){
    if (Mage::app()->getRequest()->getModuleName() == 'onepage'){
        $event = $observer->getEvent();
        $quote = $event->getQuote();
        $quoteShippingAddress = $quote->getShippingAddress();
        if (!is_null($quoteShippingAddress->getId())){
            $quoteShippingAddress->collectShippingRates()->save();
            $quoteShippingMethod = $quoteShippingAddress->getShippingMethod();
            if($quoteShippingMethod && $quoteShippingMethod == "payonpickup_payonpickup"){ 
                $cart = Mage::helper('checkout/cart')->getCart()->getQuote()->getAllItems();    
                foreach($cart as $item) {
                    $product = Mage::getModel('catalog/product')->load($item->getProductId()); 
                    $_msrpPrice = Mage::helper('core')->currency($product->getMsrp(),true,true);
                    if($_msrpPrice != ''){
                        /* apply msrp product prices */
                        $newPrice = $product->getMsrp(); //Your MSRP price 
                        $item->setCustomPrice($newPrice);
                        $item->setOriginalCustomPrice($newPrice);
                        $item->getProduct()->setIsSuperMode(true);
                        $item->save();
                    }
                }
            }else{ 
                /* apply normal product prices */

            }

        }

    }
}
  • Thank you for your answer. $quote->collectTotals()->save(); breaks page completely. – zekia May 16 at 8:26
  • @zekia , can you remove that line and test again please? – Chirag Rajput May 16 at 8:39
  • I tried that. There's no error but the code is not working properly. The quote prices don't change. – zekia May 16 at 8:54
  • I figured it out. if($_msrpPrice > 0){ was the problem. Changed it to if($_msrpPrice != ""){ and now it works – zekia May 16 at 9:08
  • @zekia , its cool. If my answer help you then you can mark it as correct answer. :) – Chirag Rajput May 16 at 9:22

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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