jdhaar
jdhaar

Reputation: 155

How to get configurable products added in cart using getQuoteItem() in Magento

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.

Upvotes: 1

Views: 4907

Answers (1)

leek
leek

Reputation: 12131

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

Upvotes: 1

Related Questions