0

I need to apply a custom tax over order based on informations provided on 'Payment Info' section, when using my custom payment method. The problem seems the call stack, the informations are collected in Mage_Payment_Model_Method_Abstract::assignData, but the tax/discount can be applied in Mage_Sales_Model_Quote_Address_Total_Abstract::collect.

There's a way to do this?

0

What you need to do is create a custom total and then check the payment method from the quote.

The below code was tested on Magento 1.9.3.1. and a 9.99 custom tax was applied to any order which used the Check/Money Order.

You just need to change the payment method in the method "collect" and the amount you want to add as a custom tax amount.

File: app/code/local/Colin/Core/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Colin_Core>
            <version>0.1.0</version>
        </Colin_Core>
    </modules>
    <global>
        <models>
            <colin_core>
                <class>Colin_Core_Model</class>
            </colin_core>
        </models>
        <blocks>
            <colin_core>
                <class>Colin_Core_Block</class>
            </colin_core>
        </blocks>
        <helpers>
            <colin_core>
                <class>Colin_Core_Helper</class>
            </colin_core>
        </helpers>

        <!-- Add custom tax total -->
        <sales>
            <quote>
                <totals>
                    <custom_tax>
                        <class>colin_core/total</class>
                        <after>subtotal,shipping</after>
                        <before>tax</before>
                    </custom_tax>
                </totals>
            </quote>
        </sales>
    </global>
</config>

File: app/code/local/Colin/Core/Model/Total.php

<?php

/**
 * Class Colin_Core_Model_Total
 *
 * @extends     Mage_Sales_Model_Quote_Address_Total_Abstract
 * @category    Colin
 * @package     Colin_Core
 */
class Colin_Core_Model_Total extends Mage_Sales_Model_Quote_Address_Total_Abstract
{

    /**
     * @return string
     */
    public function getPrice()
    {
        return "9.99";
    }

    /**
     * @return bool|string
     */
    public function getPaymentMethod()
    {
        /** @var Mage_Sales_Model_Quote_Payment $payment */
        $payment = Mage::getModel('checkout/cart')->getQuote()->getPayment();

        try {
            $method = $payment->getMethodInstance();
        } catch (Exception $e ) {
            return false;
        }

        return $method->getCode();
    }

    /**
     * @param Mage_Sales_Model_Quote_Address $address
     *
     * @return $this
     */
    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        parent::collect($address);
        if (($address->getAddressType() != 'shipping')) {
            return $this;
        }

        if ($this->getPaymentMethod() !== 'checkmo') {
            return false;
        }

        $store = Mage::app()->getStore();
        $price = $store->convertPrice("9.99");
        $basePrice = $store->convertPrice("9.99");
        $address->setCustomTaxAmount($price);
        $address->setCustomTaxBaseAmount($basePrice);
        $this->_addAmount($price);
        $this->_addBaseAmount($basePrice);
        return $this;
    }

    /**
     * @param Mage_Sales_Model_Quote_Address $address
     *
     * @return Mage_Sales_Model_Quote_Address
     */
    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
        $amount = $address->getCustomTaxAmount();
        if ($amount == 0) {
            return $address;
        }

        $title = Mage::helper('colin_core')->__('Custom Tax');
        $address->addTotal(
            array(
                'code'  => $this->getCode(),
                'title' => $title,
                'value' => $amount
            )
        );
        return $address;
    }
}

Attached is the custom tax applying to the order with check money order and not applying to the order with credit card payment method.

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.