#StackBounty: #magento-1.9 #quote #quoteitem Update quote item price without using custom price

Bounty: 50

We are developing a custom module wich applies custom tier prices by product category. Our solution works on frontend checkout so far, and consists of observing the sales_quote_item_set_product event and running custom code that checks other quote items of given category to apply a custom price.

The function responsible for applying the custom price to the item is below:

public function applyTierPrice($item, $tier)
{
    $oldPrice = (float)$item->getProduct()->getPrice();
    $newPrice = (float)$tier['discount'];
    if ($tier['type'] === 'percent') {
        $newPrice = $oldPrice - $oldPrice * ($tier['discount'] / 100);
    }

    $item->setCustomPrice($newPrice);
    $item->setOriginalCustomPrice($newPrice);

    $item->getProduct()->setIsSuperMode(true);
    $item->save();
}

This solution becomes a problem on order creation by admin panel, because there is a field where you can set manually the item price, and apparently it also uses the methods setCustomPrice().

magento-se-issue-scrot

So, when we add a product to the quote, it runs the custom applyTierPrice() method. But if i want to use that custom price field, our solution will override that field input, and we loose the default custom price functionality.

So i was thinking of a solution where we set the quote item price by calling $item->setPrice(), and not messing with custom price.

public function applyTierPrice($item, $tier)
{
    $oldPrice = (float)$item->getProduct()->getPrice();
    $newPrice = (float)$tier['discount'];
    if ($tier['type'] === 'percent') {
        $newPrice = $oldPrice - $oldPrice * ($tier['discount'] / 100);
    }

    // $item->setCustomPrice($newPrice);
    // $item->setOriginalCustomPrice($newPrice);

    $item->setPrice(42);

    $item->getProduct()->setIsSuperMode(true);
    $item->save();
}

$quote->collectTotals() and $quote->save() are being called after this function.

It updates the price field on the sales_flat_quote_item table, but the change doesn’t reflect on subtotal, row total, order total calculations.

How can i solve it? Any advice is apreciated.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.