1

I want to set custom price for all types of products. I am listening to the observer checkout_cart_product_add_after. In it's function I am using the following code to set custom price for products.

$newPrice  = $_POST["txtprice"];
$event = $observer->getEvent();
$minPrice = $observer->getProduct()->getMinPrice();
$quoteItem = $event->getQuoteItem();

if($newPrice > $minPrice){          
$quoteItem->setCustomPrice($newPrice);              
$quoteItem->setOriginalCustomPrice($newPrice);
    $quoteItem->getProduct()->setIsSuperMode(true);
}

This code works fine for simple products. For configurable products it is not working. A configurable item of the cart doesn't get set int the $quoteItem object. And so I can't get the custom price to set using $quoteItem.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jdhaar
  • 155
  • 1
  • 7
  • 21
  • `'quote_item'` is always passed to observers. See `Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));` Can you try `var_dump($event->getQuoteItem())` for configurable and show what do you see there? – Dmytro Zavalkin Feb 11 '12 at 11:21
  • Thanks Zyava for replying. var_dump() shows an extremely large output of which i don't understand anything at all. I am showing the initial two or three lines here so you know what it is. It goes something like this : `object(Mage_Sales_Model_Quote_Item)#332 (24) { ["_eventPrefix":protected]=> string(16) "sales_quote_item" ["_eventObject":protected]=> string(4) "item" ["_quote":protected]=> object(Mage_Sales_Model_Quote)#255 (20) { ["_eventPrefix":protected]=> string(11) "sales_quote" ["_eventObject":protected]=> string(5)` – jdhaar Feb 13 '12 at 04:26
  • Hmm, so `$event->getQuoteItem()` is passed to observer. So what is the difference between simple and configurable product? I understood your question as `$event->getQuoteItem()` is `null` in case of configurable product. – Dmytro Zavalkin Feb 13 '12 at 10:31
  • 1
    @jdhaar: **Quick tip:** When using `var_dump()` on any Magento classes that extend `Varien_Object`, execute the `var_dump()` like this to avoid extremely large output: `var_dump($object->debug())` – leek Feb 29 '12 at 21:35
  • Thanks leek. Will remember that. Nd @Zyava the difference is that a configurable product is an associated collection of one or more simple products. – jdhaar Mar 07 '12 at 06:56

1 Answers1

1

See the answer I've edited here:

Here is some sample code you can use within an Observer that listens for the checkout_cart_product_add_after or checkout_cart_update_items_after events. The code is logically the same except checkout_cart_product_add_after is called for only one item and checkout_cart_update_items_after is called for all items in the cart. This code is separated/duplicated into 2 methods only as an example.

For configurable products, you need to check for $item->getParentItem() as in the sample code from that answer:

Event: checkout_cart_product_add_after

/**
 * @param Varien_Event_Observer $observer
 */
public function applyDiscount(Varien_Event_Observer $observer)
{
    /* @var $item Mage_Sales_Model_Quote_Item */
    $item = $observer->getQuoteItem();
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    // Discounted 25% off
    $percentDiscount = 0.25; 

    // This makes sure the discount isn't applied over and over when refreshing
    $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

    // Make sure we don't have a negative
    if ($specialPrice > 0) {
        $item->setCustomPrice($specialPrice);
        $item->setOriginalCustomPrice($specialPrice);
        $item->getProduct()->setIsSuperMode(true);
    }
}

Event: checkout_cart_update_items_after

/**
 * @param Varien_Event_Observer $observer
 */
public function applyDiscounts(Varien_Event_Observer $observer)
{
    foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* @var $item Mage_Sales_Model_Quote_Item */) {
         if ($item->getParentItem()) {
             $item = $item->getParentItem();
         }

         // Discounted 25% off
         $percentDiscount = 0.25; 

         // This makes sure the discount isn't applied over and over when refreshing
         $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

         // Make sure we don't have a negative
         if ($specialPrice > 0) {
             $item->setCustomPrice($specialPrice);
             $item->setOriginalCustomPrice($specialPrice);
             $item->getProduct()->setIsSuperMode(true);
         }
    }
}
Community
  • 1
  • 1
leek
  • 11,803
  • 8
  • 45
  • 61