Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Magento Set a Quote Item's Price?

Tags:

oop

php

magento

Whenever you load the cart page in Magento, the following code is run

$cart->init();
$cart->save(); 

One side effect of this is that the prices for any items in the cart are updated if the price of the product has been updated. This actually updates the entry in sales_flat_quote_item. I'm trying to track down where in code the price is updated on each quote item, and where each quote item is saved.

I know the myrid locations it could be set. I'm hoping someone knows where it actually is set. Magento 1.7x branch specifically, although info from all versions is welcome.

like image 357
Alan Storm Avatar asked Jul 18 '12 19:07

Alan Storm


People also ask

What is quote in Magento?

Magento uses a quote to perform tasks such as. Track each item the customer wants to buy, including the quantity and base cost. Gather information about the customer, including billing and shipping addresses.

How can I get product price in Magento 2?

All you need to is inject \Magento\Catalog\Model\ProductRepository class in your construct. Using getSpecialPriceByProId($proId) function with pass product ID as Parameter, you can get the product's special price by product id.

What is quote table in Magento 2?

The quote table ( sales_flat_quote on M1) contains records on every shopping cart created in your store, whether they were abandoned or converted to a purchase. Each row represents one cart.

What is final price in magento2?

7. Final Price. The final price is the total price of products and depends on each product type. For simple, virtual, and downloadable products: the final price is the lowest price among regular_price, catalog_rule_price, special_price, and tier_price.


2 Answers

Dug this one up myself. So there's this

#File: app/code/core/Mage/Sales/Model/Quote.php
foreach ($this->getAllAddresses() as $address) {
    ...
    $address->collectTotals();
    ...
}    

which leads to this

#File: app/code/core/Mage/Sales/Model/Quote/Address.php
public function collectTotals()
{
    Mage::dispatchEvent($this->_eventPrefix . '_collect_totals_before', array($this->_eventObject => $this));
    foreach ($this->getTotalCollector()->getCollectors() as $model) {
        $model->collect($this);            
    }
    Mage::dispatchEvent($this->_eventPrefix . '_collect_totals_after', array($this->_eventObject => $this));
    return $this;
}

The getTotalCollector object returns a sales/quote_address_total_collector object, which loads a series of collector models from global/sales/quote/totals and calls collect on them. The sub-total collector's collect method ultimately calls this

#File: app/code/core/Mage/Sales/Model/Quote/Address/Total/Subtotal.php
protected function _initItem($address, $item)
{
    //...
    if ($quoteItem->getParentItem() && $quoteItem->isChildrenCalculated()) {
        $finalPrice = $quoteItem->getParentItem()->getProduct()->getPriceModel()->getChildFinalPrice(
           $quoteItem->getParentItem()->getProduct(),
           $quoteItem->getParentItem()->getQty(),
           $quoteItem->getProduct(),
           $quoteItem->getQty()
        );
        $item->setPrice($finalPrice)
            ->setBaseOriginalPrice($finalPrice);
        $item->calcRowTotal();
    } else if (!$quoteItem->getParentItem()) {
        $finalPrice = $product->getFinalPrice($quoteItem->getQty());
        $item->setPrice($finalPrice)
            ->setBaseOriginalPrice($finalPrice);
        $item->calcRowTotal();
        $this->_addAmount($item->getRowTotal());
        $this->_addBaseAmount($item->getBaseRowTotal());
        $address->setTotalQty($address->getTotalQty() + $item->getQty());
    }    
    //...
}

and this is where the quote item gets it's price set/rest.

like image 122
Alan Storm Avatar answered Oct 26 '22 02:10

Alan Storm


From a high level, the code that starts the whole process are lines 464 and 465 of Mage_Checkout_Model_Cart :

 $this->getQuote()->collectTotals();
 $this->getQuote()->save();

The new product price is being set against the quote in Mage_Sales_Model_Quote_Address_Total_Subtotal in the _initItem method. You will see $item->setPrice in the the if / else statement starting at line 104

like image 23
Drew Hunter Avatar answered Oct 26 '22 03:10

Drew Hunter