简体   繁体   English

Magento 1 - Magento 在哪里设置报价项目的价格?

[英]Magento 1 - Where does Magento Set a Quote Item's Price?

Whenever you load the cart page in Magento, the following code is run每当您在 Magento 中加载购物车页面时,都会运行以下代码

$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 .这实际上更新了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. Magento 1.7x 特别分支,尽管欢迎所有版本的信息。

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. getTotalCollector对象返回一个sales/quote_address_total_collector对象,该对象从global/sales/quote/totals加载一系列收集器模型并调用它们的collect The sub-total collector's collect method ultimately calls this小计收集器的collect方法最终调用了这个

#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.这是报价项目获取其价格设置/休息的地方。

From a high level, the code that starts the whole process are lines 464 and 465 of Mage_Checkout_Model_Cart :从高层来看,启动整个过程的代码是Mage_Checkout_Model_Cart第 464 和 465 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.新产品价格是根据_initItem方法中Mage_Sales_Model_Quote_Address_Total_Subtotal中的报价设置的。 You will see $item->setPrice in the the if / else statement starting at line 104您将在 if / else 语句中从第 104 行开始看到$item->setPrice

I don't know if this helps you all that much, but if you are trying to do custom price changes on products in the cart, rather than extend and modify core classes, I use an observer sales_quote_save_before.我不知道这是否对您有很大帮助,但是如果您尝试对购物车中的产品进行自定义价格更改,而不是扩展和修改核心类,我会使用观察者 sales_quote_save_before。 It works great if you are trying to customize pricing (especially when I have products that can be custom length).如果您尝试自定义定价(尤其是当我有可以自定义长度的产品时),它会非常有效。 I have code examples if you want them.如果您需要,我有代码示例。

But I am talking to THE Alan Storm here, so you may laugh at my overly simplistic answer.但我在这里和艾伦风暴谈话,所以你可能会嘲笑我过于简单的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM