【问题标题】:Where does Magento Set a Quote Item's Price?Magento 在哪里设置报价项目的价格?
【发布时间】:2012-07-17 22:02:49
【问题描述】:

每当您在 Magento 中加载购物车页面时,都会运行以下代码

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

这样做的一个副作用是,如果产品的价格已更新,则购物车中任何商品的价格都会更新。这实际上更新了sales_flat_quote_item 中的条目。我正在尝试追踪在代码中每个报价项目的价格更新位置,以及每个报价项目的保存位置。

我知道它可以设置的无数个位置。我希望有人知道它实际设置在哪里。 Magento 1.7x 分支,尽管欢迎来自所有版本的信息。

【问题讨论】:

    标签: php oop magento


    【解决方案1】:

    如果您尝试对购物车中的产品进行自定义价格更改,而不是扩展和修改核心类,我使用观察者 sales_quote_save_before。如果您尝试自定义定价(尤其是当我有可以自定义长度的产品时),它会非常有用。

    【讨论】:

      【解决方案2】:

      我自己挖了这个。所以有这个

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

      导致这个

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

      getTotalCollector 对象返回一个sales/quote_address_total_collector 对象,它从global/sales/quote/totals 加载一系列收集器模型并在它们上调用collect。小计收集器的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());
          }    
          //...
      }
      

      这是报价项目获取其价格设置/休息的地方。

      【讨论】:

        【解决方案3】:

        从高层次上看,启动整个过程的代码是Mage_Checkout_Model_Cart的第464和465行:

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

        新产品价格是根据Mage_Sales_Model_Quote_Address_Total_Subtotal 中的报价在_initItem 方法中设置的。您将在从第 104 行开始的 if / else 语句中看到 $item->setPrice

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多