phpmagentoobservers

Magento | Update Item Price in Quote Loop


I have a bit of custom pricing that needs to be carried out for each product in an order which alters the end price of the item.

I am using an observer with the sales_quote_item_qty_set_after event to ensure that the price is run each time the product is re-configured, as I haven't been able to get desired results from other events.

The issue i'm running into is the inability to change the price of the product in the quote when looping through the quote using ->getAllItems().

I know that the price can be overridden via the following for the product add events.

$_item = $obs->getQuoteItem();
$_item = ( $_item->getParentItem() ? $_item->getParentItem() : $_item );
$new_price = 1000;
$_item->setCustomPrice($new_price);
$_item->setOriginalCustomPrice($new_price);
$_item->getProduct()->setIsSuperMode(true);

However, I can not find a way to do it when running through the quote items within the cart. Is this possible, or would the pricing have to be done in a different observer, and if so, which?

Full code is below...

public function adminAddProduct(Varien_Event_Observer $obs) {
    $quote = $this->_getSession()->getQuote();
    foreach($quote->getAllItems() as $_item){
        $options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
        $_product = $_item->getProduct();
        $sku = $options['simple_sku'];
        if ($sku) {
            $tierPrices = $_product->getTierPrice();
            $price = $_item->getPrice();
            $qty = $options['info_buyRequest']['qty'];

            $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
            $runspeed = $product->getResource()->getAttribute('runspeed')->getFrontend()->getValue($product);

            $prodOpts = [];
            foreach($options['options'] as $option) {
                $prodOpts[$option['label']] = $option['value'];
            }

            $index = 0;
            foreach($tierPrices as $tierPrice) {
                if($tierPrice['price_qty'] <= $qty && $tierPrices[$index + 1]['price_qty'] > $qty) {
                    $price = $tierPrice['price'];
                }
                //$prodTiers[$tierPrice['price_qty']] = $tierPrice['price'];
            }

            if($prodOpts['Quantity'] == 'Fixed Quantity' && array_key_exists('Quantity Select', $prodOpts)){
                $qty = str_replace(',', '', explode(" ", $prodOpts['Quantity Select'])[0]);
            } else {
                $qty = 1;
            }

            $_item->setQty($qty);
            $_item->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice);

            $new_price = 999;

            $_item->setPrice($new_price);
            $_item->setCustomPrice($new_price);
            $_item->setOriginalCustomPrice($new_price);
            $_item->save();

            Mage::log($price);
            Mage::log($prodOpts);
        }
    }
}

Any help is appreciated.


Solution

  • You might try $quote->save() at the end of your function.