2011-09-01 91 views

回答

9

您可以使用一个观察者类听checkout_cart_product_add_after,并使用产品的“超级模式”设置自定义价格对报价项目。

在你/app/code/local/{namespace}/{yourmodule}/etc/config.xml:

<config> 
    ... 
    <frontend> 
     ... 
     <events> 
      <checkout_cart_product_add_after> 
       <observers> 
        <unique_event_name> 
         <class>{{modulename}}/observer</class> 
         <method>modifyPrice</method> 
        </unique_event_name> 
       </observers> 
      </checkout_cart_product_add_after> 
     </events> 
     ... 
    </frontend> 
    ... 
</config> 

然后在创建/应用/代码观察员类/本地/ {命名空间}/{} yourmodule /Model/Observer.php

<?php 
    class <namespace>_<modulename>_Model_Observer 
    { 
     public function modifyPrice(Varien_Event_Observer $obs) 
     { 
      // Get the quote item 
      $item = $obs->getQuoteItem(); 
      // Ensure we have the parent item, if it has one 
      $item = ($item->getParentItem() ? $item->getParentItem() : $item); 
      // Load the custom price 
      $price = $this->_getPriceByItem($item); 
      // Set the custom price 
      $item->setCustomPrice($price); 
      $item->setOriginalCustomPrice($price); 
      // Enable super mode on the product. 
      $item->getProduct()->setIsSuperMode(true); 
     } 

     protected function _getPriceByItem(Mage_Sales_Model_Quote_Item $item) 
     { 
      $price; 

      //use $item to determine your custom price. 

      return $price; 
     } 

    } 
+0

如果我添加使用观察者设置自定义价格到购物车,然后我得到以电子邮件模板乘以项目数量的单位价格。 我该如何解决这个问题 app \ design \ frontend \ default \ rfg \ template \ email \ order \ items \ order \ default.phtml <?php echo $ _order-> formatPrice($ _ item-> getRowTotal())? > – Muk

+0

任何人都可以解释从该行''setIsSuperMode' $用品 - > getProduct() - > setIsSuperMode(真);'?它有什么作用? – mkutyba

+2

这里是超模的explantion: “报价超级模式标志是什么意思,我们与报价工作,不受任何限制” 例如:如果超级模式设置为true,磁不会检查该产品是否在目录中可见 – Nidheesh

相关问题