Magento: tax is incorrect for an extra charge at checkout

I added an additional fee to my design, but the tax is not calculated correctly.

The tax amount does not add up correctly if I do $ this → _ calculateTax ($ address); Amount including tax is added to the collection function, but my tax amount remains.

if the var_dump set applied taxes after the line $ address-> setAppliedTaxes ($ previouslyAppliedTaxes); I see the correct amount. He dumps it twice, the first time I see the correct amount of tax only for my additional fee, the second - the correct amount of total tax. But in the interface, it shows tax-free tax on my extra charge.

Is it possible to understand what this could be?

class Company_Customer_Model_Quote_Address_Total_PrintPrepCosts extends Mage_Sales_Model_Quote_Address_Total_Abstract { public function __construct() { $this->setCode('printPrepCosts'); $this->_store = Mage::app()->getStore(); return $this; } public function collect(Mage_Sales_Model_Quote_Address $address) { parent::collect($address); $address->setPrintPrepcosts(0); $address->setTaxAmount(0); $address->setBaseTaxAmount(0); if(count($address->getAllItems()) == 0) { return $this; } $pricePrint = $this->calcTotalPrintPrepCosts(); $address->setPrintPrepcosts($pricePrint); $address->setBasePrintPrepcosts($pricePrint); $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getPrintPrepcosts()); $address->setGrandTotal($address->getGrandTotal() + $address->getPrintPrepcosts()); $this->_calculateTax($address); return $this; } protected function _calculateTax(Mage_Sales_Model_Quote_Address $address) { $calculator = Mage::getSingleton('tax/calculation'); $inclTax = Mage::getStoreConfig('tax/calculation/printing_prep_includes_tax', $this->_store); $taxRateRequest = $calculator->getRateRequest( $address, $address->getQuote()->getBillingAddress(), $address->getQuote()->getCustomerTaxClassId(), $this->_store ); // TODO undef prop _store $taxRateRequest->setProductClassId(Mage::getStoreConfig('tax/classes/printing_prep_tax_class', $this->_store)); $rate = $calculator->getRate($taxRateRequest); $baseTax = $tax = $calculator->calcTaxAmount($address->getPrepPrintcosts(), $rate, $inclTax, true); $address->addTotalAmount('tax', max(0, $tax)); $address->addBaseTotalAmount('tax', max(0, $baseTax)); $this->_saveAppliedTaxes($address, $calculator->getAppliedRates($taxRateRequest), $tax, $baseTax, $rate ); // later on added - which fixes the total, lose tax amount still off $address->setTaxAmount($tax); $address->setBaseTaxAmount($baseTax); if($inclTax) { $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseTax); $address->setGrandTotal($address->getGrandTotal() - $tax); } } protected function _saveAppliedTaxes(Mage_Sales_Model_Quote_Address $address, $applied, $amount, $baseAmount, $rate) { $previouslyAppliedTaxes = $address->getAppliedTaxes(); $process = count($previouslyAppliedTaxes); foreach ($applied as $row) { if (!isset($previouslyAppliedTaxes[$row['id']])) { $row['process'] = $process; $row['amount'] = 0; $row['base_amount'] = 0; $previouslyAppliedTaxes[$row['id']] = $row; } if (!is_null($row['percent'])) { $row['percent'] = $row['percent'] ? $row['percent'] : 1; $rate = $rate ? $rate : 1; $appliedAmount = $amount/$rate*$row['percent']; $baseAppliedAmount = $baseAmount/$rate*$row['percent']; } else { $appliedAmount = 0; $baseAppliedAmount = 0; foreach ($row['rates'] as $rate) { $appliedAmount += $rate['amount']; $baseAppliedAmount += $rate['base_amount']; } } if ($appliedAmount || $previouslyAppliedTaxes[$row['id']]['amount']) { $previouslyAppliedTaxes[$row['id']]['amount'] += $appliedAmount; $previouslyAppliedTaxes[$row['id']]['base_amount'] += $baseAppliedAmount; } else { unset($previouslyAppliedTaxes[$row['id']]); } } $address->setAppliedTaxes($previouslyAppliedTaxes); } public function fetch(Mage_Sales_Model_Quote_Address $address) { $address->addTotal(array( 'code' => $this->getCode(), 'title' => "Prep Print costs", 'value' => $address->getPrintPrepcosts(), )); return $this; } 

EDIT 1 XML contains the following:

  <sales> <quote> <totals> <printPrepCosts> <class>Company_Customer_Model_Quote_Address_Total_PrintPrepCosts</class> <after>subtotal</after> <before>tax</before> </printPrepCosts> </totals> </quote> </sales> 

EDIT 2 I added the following lines to my calcTax function, this captures the total, but the TAX amount is still disabled.

  $address->setTaxAmount($tax); $address->setBaseTaxAmount($baseTax); 

OUTPUT example

 Subtotal € 67,50 printPrepCosts € 40,00 Shipping € 50,00 TAX € 22,33 Total € 187,43 

EDIT 3 My bad, only GoMage onepage check gives the correct total, not in the cart. Regularly checking Magento onepage also gives the wrong grand file.

+8
php magento
source share
3 answers

I found a workaround, this is not the best solution, but it works :)

In my address, I set the tax on an additional var tax tax and use it in the supervisor to manipulate the tax.

 class Company_Client_Model_Observer { public function setCorrectTax ($observer) { $quote = $observer->getQuote(); foreach ($quote->getAllAddresses() as $address) { $printPrepCosts = $address->getPrintPrepcosts(); if(!empty($printPrepCosts)) { $address->setTaxAmount($address->tax); } } } } 

And XML

  <events> <sales_quote_collect_totals_after> <observers> <client> <type>singleton</type> <class>client/observer</class> <method>setCorrectTax</method> </client> </observers> </sales_quote_collect_totals_after> </events> 
+5
source share

Are you trying to change an existing tax position or add a new one? Can you show your config.xml entry for a generic model?

If you add a new tax position, this should be enough, just test it against installing the CE 1.7 valine:

 class KJ_Mymodule_Model_Sales_Quote_Address_Total_Kjtest extends Mage_Sales_Model_Quote_Address_Total_Abstract { public function __construct() { $this->setCode('kjtest'); } public function collect(Mage_Sales_Model_Quote_Address $address) { parent::collect($address); $this->_setAmount(1.01); $this->_setBaseAmount(1.01); return $this; } public function processConfigArray($config, $store) { return $config; } public function fetch(Mage_Sales_Model_Quote_Address $address) { $address->addTotal(array( 'code' => $this->getCode(), 'title' => "Prep Print costs", 'value' => 1.01, )); return $this; } } 

Enough, I mean, that this should be the minimum minimum required to get this tax position.

With this in your config.xml file:

 <sales> <quote> <totals> <kjtest> <class>mymodule/sales_quote_address_total_kjtest</class> <after>tax</after> </kjtest> </totals> </quote> </sales> 
+2
source share

Read below. I think this helps you a lot: -

See address below

http://www.excellencemagentoblog.com/magento-add-fee-discount-order-total

Magento Add total amount or discount

In this tutorial, we will see how to add a new line item to magento totals. This means that how to add an additional fee or discount, or any type of charging the order amount to the purple process. In a typical order, the total order amounts usually include: “Total”, “Delivery cost”, “Taxes”, “Discount”, based on these values, the total amount of orders is calculated. Now, if we want to add an additional charge on a credit card or a discount on a discount or a discount on an affiliate program or any other order amount that will affect the total order quantity, we need to create a magenta module. This additional fee, which we add to the total amount, will be reflected in

  • Place an order to order
  • Cart Order page Total
  • My Account View Account
  • Order EMails
  • View Admin Order / Email / PDF
  • View Admin Invoice / Email / PDF
  • View Credit Loan Admin / Email / PDF

as you can see from the above list, this module is not simple. In this tutorial, I attach the source of such a very basic module that you can use as a starting point to add additional changes. I will also explain the basics of how to implement this. In this tutorial, I will add a new number of orders called a $ 10 fixed cost fee.

Or try:

Place an order Total amount of total orders

We will see how to add totals only to the verification page. All summary items displaying the verification page come from files located in the folder Mage \ Sales \ Model \ Quote \ Address \ Total. In purple, before placing an order, all order data is stored in the quotation object and, after placing the order, it is transmitted to the order object. The resulting quotes follow the pattern of the collector, and we can add collectors as many collectible classes. To add a collector to the quote object in our config.xml file, we add lines

 <global> <sales> <quote> <totals> <fee> <class>fee/sales_quote_address_total_fee</class> </fee> </totals> </quote> </sales> </global> 

This means that whenever the totals are computed for a quote, it also calls this class. All collectors are called from the collectTotals () function in the Quote model. In our collector class, we enter the code

 <?php class Excellence_Fee_Model_Sales_Quote_Address_Total_Fee extends Mage_Sales_Model_Quote_Address_Total_Abstract{ protected $_code = 'fee'; public function collect(Mage_Sales_Model_Quote_Address $address) { parent::collect($address); $this->_setAmount(0); $this->_setBaseAmount(0); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; //this makes only address type shipping to come through } $quote = $address->getQuote(); if(Excellence_Fee_Model_Fee::canApply($address)){ //your business logic $exist_amount = $quote->getFeeAmount(); $fee = Excellence_Fee_Model_Fee::getFee(); $balance = $fee - $exist_amount; $address->setFeeAmount($balance); $address->setBaseFeeAmount($balance); $quote->setFeeAmount($balance); $address->setGrandTotal($address->getGrandTotal() + $address->getFeeAmount()); $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBaseFeeAmount()); } } public function fetch(Mage_Sales_Model_Quote_Address $address) { $amt = $address->getFeeAmount(); $address->addTotal(array( 'code'=>$this->getCode(), 'title'=>Mage::helper('fee')->__('Fee'), 'value'=> $amt )); return $this; } } 

The two main functions here are collect () and fetch (). In the collection function, you add any amount that you want to receive in the total amounts of the order, and the fetch () function is used for display. If done correctly, you should see your general order line on the checkout and cart page. Here we use two fields fee_amount and base_fee_amount, which contain our fee amount. We will need to save these two fields in the database, so in our module installer file we will add this code

 ALTER TABLE `".$this->getTable('sales/quote_address')."` ADD `fee_amount` DECIMAL( 10, 2 ) NOT NULL; ALTER TABLE `".$this->getTable('sales/quote_address')."` ADD `base_fee_amount` DECIMAL( 10, 2 ) NOT NULL; 

Order page

So far, all written code has been executed only for the citation object. But after placing the order, we need to transfer all the information to the order object. As you saw above, we use the two fields fee_amount and base_fee_amount, now we also need to save these two fields in the order table. To do all of the above, we need to do two things. First, in the config.xml file, add this code inside the global tab,

 <fieldsets> <sales_convert_quote_address> <fee_amount><to_order>*</to_order></fee_amount> <base_fee_amount><to_order>*</to_order></base_fee_amount> </sales_convert_quote_address> </fieldsets> 

and in our module installation file

 ALTER TABLE `".$this->getTable('sales/order')."` ADD `fee_amount` DECIMAL( 10, 2 ) NOT NULL; ALTER TABLE `".$this->getTable('sales/order')."` ADD `base_fee_amount` DECIMAL( 10, 2 ) NOT NULL; 

After that, these two fields should be saved in the order table from the quote table.

These are just the basics of adding a line item to order everything. There is still a lot of code written inside the attached module, please read it in detail to understand more.

+2
source share

All Articles