Magento: Custom totals counted twice?

Well, I created my own general class to add a special discount, and everything seems to work fine, except for some reason I can’t find, my amount is calculated twice! This leads to double the amount of discounts and the wrong total amount. Now this happens on the cart page and on the checkout pages ... BUT ... when I complete the order, the amount is in order, calculated only once, and the total amount is in order.

It is strange that the collect method is called twice for pages with a cart, but only once when the order ends, but I can "track where all this happens and why."

To skip the junk code, I insert only important

<sales> <quote> <totals> <mydiscount> <class>ucon_mydiscount/total_mydiscount</class> <before>subtotal</before> </mydiscount> </totals> </quote> </sales> 

and collector methods

  public function collect(Mage_Sales_Model_Quote_Address $address) { parent::collect($address); $quote = $address->getQuote(); $quoteId = $quote->getEntityId(); $items = $quote->getAllItems(); if (!count($items)) { return $this; } $discount = 0; $productId = 2556; foreach($items as $item) { if($item->getProduct()->getId() == $productId) { $qty = $item->getQty(); $totalPrice = round(($item->getRowTotal()+$item->getTaxAmount()),2); //discount 10% $discount = round($totalPrice * 0.1,2); $discount = 0 - $discount; } } if($discount == 0) return $this; $this->_setAmount($discount); $this->_setBaseAmount($discount); return $this; } 

and fetcher

  public function fetch(Mage_Sales_Model_Quote_Address $address) { $amount = $address->getMydiscountAmount(); if ($amount != 0) { $title = Mage::helper('ucon_mydiscount')->__('My discount'); $address->addTotal(array( 'code' => $this->getCode(), 'title' => $title, 'value' => $amount )); } return $this; } 

edit: Another thing that I find very strange is that I am doing setValue in the collect method, not addValue, so even if the method is called twice, it should not be a double value, it should just set it twice to the correct value.

+4
source share
3 answers

Could the problem be that the shared entity belongs to the addressed entity, and Magento orders usually have two addresses β€” one for sending and one for billing?

Thus, your total amount will be called up for launch twice - once with the invoicing address and once with the delivery address, and the amount is summed up for each address. You can check which address you received and only apply the value to one of them, for example:

 public function collect(Mage_Sales_Model_Quote_Address $address) { $this->_setAddress($address); $this->_setAmount(0); $this->_setBaseAmount(0); if ($address->getAddressType() == 'shipping') { //only apply an actual value to the shipping address //... Do your calculation here as above ... } return $this; } 

You will also need to do something similar in the fetch method ...

 public function fetch(Mage_Sales_Model_Quote_Address $address) { $amount = $address->getMydiscountAmount(); if ($amount != 0 && $address->getAddressType() == 'shipping') { $title = Mage::helper('ucon_mydiscount')->__('My discount'); $address->addTotal(array( 'code' => $this->getCode(), 'title' => $title, 'value' => $amount )); } return $this; } 

I admit that the collect function may be prettier, but I hope you get the idea anyway.

Try this and see if your totals in the interface and admin area match correctly.

+15
source

After searching, here is another solution

 public function collect(Mage_Sales_Model_Quote_Address $address) { parent::collect($address); //Pay attention to this code $items = $this->_getAddressItems($address); if (!count($items)) { return $this; //this makes only address type shipping to come through } //Do whatever you want here to add discount or fee... return $this; } 

Thus, a discount or fee will be added only to the delivery address, and it will be calculated once. Therefore, we do not even need to add if ($address->getAddressType() == 'shipping') { in the fetch function.

+1
source

Is it possible that you add your own layout XML for the shopping cart block? If so, there is a chance that the block is called twice (one of the base code and again for your code - even if you just extend it), thereby duplicating the total price. If so, you will need to remove (destructively with the <remove> ) the basic xml layout for this block, and then everything should fall into place and work.

0
source

Source: https://habr.com/ru/post/1382943/


All Articles