简体   繁体   English

在购物车和发票中获得总额

[英]get round grand total in cart & invoice

I want rounding grand total So, I have create custom module and rewrite core Models 我想要舍入总计所以,我已经创建了自定义模块并重写了核心模型

My rewrite Model code is below 我的重写模型代码如下

1. Mage_Sales_Model_Quote_Address_Total_Grand 1. Mage_Sales_Model_Quote_Address_Total_Grand

<?php
class Lr_Roundtotal_Model_Quote_Address_Total_Grand extends Mage_Sales_Model_Quote_Address_Total_Grand
{
    public function collect(Mage_Sales_Model_Quote_Address $address)    
    {        
        $grandTotal     = $address->getGrandTotal();        
        $baseGrandTotal = $address->getBaseGrandTotal();        
        $totals     = array_sum($address->getAllTotalAmounts());        
        $baseTotals = array_sum($address->getAllBaseTotalAmounts());        
        $address->setGrandTotal(round($grandTotal+$totals)); //Modificated        
        $address->setBaseGrandTotal(round($baseGrandTotal+$baseTotals)); //Modificated        
        //$address->setGrandTotal($grandTotal+$totals);  --Original        
        //$address->setBaseGrandTotal($baseGrandTotal+$baseTotals); --Original        
        return $this;    
    } 

    public function fetch(Mage_Sales_Model_Quote_Address $address)    
    {        
        $address->addTotal(array(            
            'code'  => $this->getCode(),            
            'title' => Mage::helper('sales')->__('Grand Total'),            
            'value' => round($address->getGrandTotal()),            
            'netvalue' => round($address->getGrandTotal()),            
            'area'  => 'footer',        
        ));                
        return $this;    
    }   
}

and second one is 第二个是

2.Mage_Sales_Model_Order_Invoice 2.Mage_Sales_Model_Order_Invoice

<?php
    class Lr_Roundtotal_Model_Order_Invoice extends Mage_Sales_Model_Order_Invoice
    {
        public function pay()
        {
            if ($this->_wasPayCalled) {
                return $this;
            }
            $this->_wasPayCalled = true;

            $invoiceState = self::STATE_PAID;
            if ($this->getOrder()->getPayment()->hasForcedState()) {
                $invoiceState = $this->getOrder()->getPayment()->getForcedState();
            }

            $this->setState($invoiceState);

            $this->getOrder()->getPayment()->pay($this);
            $this->getOrder()->setTotalPaid(
                round($this->getOrder()->getTotalPaid()+$this->getGrandTotal())  //Modificated 
                // $this->getOrder()->getTotalPaid()+$this->getGrandTotal()  --Original
            );
            $this->getOrder()->setBaseTotalPaid(
                round($this->getOrder()->getBaseTotalPaid()+$this->getBaseGrandTotal())  //Modificated 
                // $this->getOrder()->getBaseTotalPaid()+$this->getBaseGrandTotal()  --Original
            );
            Mage::dispatchEvent('sales_order_invoice_pay', array($this->_eventObject=>$this));
            return $this;
        }   
    }

=>For example =>例如

Cart 大车

sub-total : 990.00 小计:990.00

discount : 120.70 折扣:120.70

Grand Total: 869.00 (rounded) 总计: 869.00 (四舍五入)

Invoice 发票

sub-total : 990.00 小计:990.00

discount : 120.70 折扣:120.70

Grand Total: 869.30 (not-rounded) 总计: 869.30 (未圆)

I want same grand total in cart and Invoice So, please help me 我想在购物车和Invoice中获得相同的总额,请帮助我

Thanks in advance 提前致谢

Once you overrite the core file in local then implement below code. 一旦你在本地覆盖核心文件然后实现下面的代码。

First Create one function in the HELPER file like below(no need to create new module but you can put this function in any module helper file) : 首先在HELPER文件中创建一个函数,如下所示(无需创建新模块,但可以将此函数放在任何模块帮助文件中):

Namespace_module_helper_data extends Mage_Core_Helper_Abstract 
{
   public function getFormatedPrice($price)     
   {
     return Mage::getModel('directory/currency')->format($price, array('display'=>Zend_Currency::NO_SYMBOL), false);    
   } 
}

then you can just use this function where you need to round the price in any where in magento. 然后你可以使用这个功能,你需要在magento的任何地方四舍五入。

You can use helper function like below : 你可以使用如下的帮助函数:

$helper = Mage::helper('modulename'); // module name means name of the module in which you have create helper

Use function for round price like below : 如下所示使用圆形价格函数:

$price = 120.12456;
echo $helper->getFormatedPrice($price); // you can get round price as per your store.

Thanks 谢谢

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

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