Modifying the Price of a Cart Item

Magento provides a relatively easy way to modify the price of a cart item:

config.xml

<checkout_cart_product_add_after>
	<observers>
		<llapgoch_modify_add_to_cart_price>
			<model>llapgoch_pricetest/observer</model>
			<method>modifyItem</method>
			<type>model</type>
		</llapgoch_modify_add_to_cart_price>
	</observers>
</checkout_cart_product_add_after>

– The checkout_cart_product_add_after passes an instance of the product being added and the quote order item to the observer.

The Observer Method

<?php
public function modifyItem($observer){
	$item = $observer->getQuoteItem();
		
	$item = $item->getParentItem() ? $item->getParentItem() : $item; 
		
	$item->setCustomPrice(2000);
	$item->setOriginalCustomPrice(2000);
	$item->getProduct()->setIsSuperMode(true);		
}

– This will set the cart item to have a price of 2000.
– Remember to get the parent item of the cart item if we’re adding configurables to the cart.
– I’m not entirely sure what the setIsSuperMode does, it only appears in Magento’s core code once and doesn’t appear to be used anywhere else (unless I’m missing something), so it’s only here for completeness.