1
votes

I have created one coupon with "Fixed Amount" that is suppose to $100 and I am adding product to the cart which price is $100. Now I have selected in backend in System->configuration->tax->calculation setting "Apply Customer Tax is After Discount" and "Apply Discount on price is Excluding tax".

Now below is scenarios : 1) When I am adding product to cart and applying coupon then grand total become 0.(because coupon value 100 and Product price is 100 so it's become 0) now if I press on checkout button,in checkout page price displaying $0 and tax also showing $0.

I want to apply tax once if I apply coupon and my tax should get calculated based on some amount that is $40(for example). if I have created tax rule for state with amount 10% then my tax should calculated on $40*10/100 = $4 should tax added to total before clicking to checkout button.

Is anyone know in which observer I need to take look up for adding those conditions.

I have tried following things : I am observing following observer :

        <checkout_cart_save_after>
            <observers>
                <NamespaceModulename>
                    <type>singleton</type>
                    <class>Namespace_Modulename_Model_Observer</class>
                    <method>updateTaxAfterDiscountApplied</method>
                </NamespaceModulename>
            </observers>
        </checkout_cart_save_after>

and following is my method code :

public function updateTaxAfterDiscountApplied()
{
    $quote = Mage::getModel('checkout/cart')->getQuote();
    $coupon_code = $quote->getCouponCode();

    if($coupon_code)
    {
        $coupon_rule_id = $quote->getAppliedRuleIds();
        echo 'rule id'.$coupon_rule_id;
        echo $coupon_code.'couponcode';
   }
   if($coupon_rule_id){
        $con = Mage::getSingleton('core/resource')->getConnection('core_read');
        $sale_rule_result = $con->fetchAll("SELECT * FROM salesrule where rule_id=".$coupon_rule_id);
        foreach($sale_rule_result as $rule){
            echo 'tax price'.$rule['tax_calculation_price'];
        }
    }

}

in above method "tax_calculation_price" is my custom field which is used when I apply coupon and my grand total zero. then I have this field which is used to calculate tax on based on tax calculation. for example tax_calculation_price = 40 then and for suppose canada->ontario my tax rate is 12% then it should calculate 12% of 40 and add to grand total. so how to fetch tax so that I can calculate it with tax_calculation_price. ? any help appreciate.

2

2 Answers

0
votes

You can use this obeserver event

<global>
<events>
    <salesrule_validator_process>
        <observers>
            <Test_Mymodule_Hook>
                <type>singleton</type>
                <class>Test_Mymodule_Model_Observer</class>
                <method>specialpricediscount</method>
            </Test_Mymodule_Hook>
        </observers>
    </salesrule_validator_process>
</events>
</global> 
0
votes

Finally I got the answer of my question. Here I am giving code to update tax after applying your coupon if total zero then also it will update tax. write below code in config.xml inside global node.

    <events>
         <sales_quote_collect_totals_after>
            <observers>
                <NamespaceModulename>
                    <type>singleton</type>
                    <class>Namespace_Modulename_Model_Observer</class>
                    <method>updateTaxAfterDiscountApplied</method>
                </NamespaceModulename>
            </observers>
        </sales_quote_collect_totals_after> 
    </events>

Created observer.php and added following method:

//Apply tax after discount even if grand total 0.
public function updateTaxAfterDiscountApplied()
{   
    $billing_data = Mage::app()->getRequest()->getPost('billing', array());
    $shipping_data = Mage::app()->getRequest()->getPost('shipping', array());

    $quote = Mage::getModel('checkout/cart')->getQuote();
    $coupon_code = $quote->getCouponCode();

    if($coupon_code)
    {
        $coupon_rule_id = $quote->getAppliedRuleIds();          
        if($coupon_rule_id){
            $con = Mage::getSingleton('core/resource')->getConnection('core_read'); // fetching custom amount value to calculate with tax
            $sale_rule_result = $con->fetchAll("SELECT * FROM salesrule where rule_id=".$coupon_rule_id);
            foreach($sale_rule_result as $rule){
                $tax_calculation_based_on_price = $rule['tax_calculation_price'];               
            }
        }       
        $country = $billing_data['country_id'];
        $region_id = $billing_data['region_id'];
        $TaxRequest  = new Varien_Object();
        $TaxRequest->setCountryId( $country );
        $TaxRequest->setRegionId( $region_id );
        //$TaxRequest->setPostcode( $postcode );
        $TaxRequest->setStore( Mage::app()->getStore() );
        $TaxRequest->setCustomerClassId(3);
        $TaxRequest->setProductClassId(5);  

        $taxCalculationModel = Mage::getSingleton('tax/calculation');
        $rate = $taxCalculationModel->getRate($TaxRequest);
        $tax_percentage = (($tax_calculation_based_on_price * $rate)/100);
        $q=$quote;
        $f=0;
        foreach ($q->getAllAddresses() as $address) {
        if($f)continue;
            $address->setTaxAmount($tax_percentage);
            $address->setBaseTaxAmount($tax_percentage);
            Mage::log('ww'.$address->getTaxAmount(),null,'billing.log');
            Mage::log('pw'.$q->getTaxAmount(),null,'billing.log');
            $address->setGrandTotal($address->getGrandTotal() + $address->getTaxAmount());
            $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBaseTaxAmount());
                $f=1;
        }               
    } 
} 

for more information for Magento Events cheat-sheet follow below link: https://www.nicksays.co.uk/magento-events-cheat-sheet-1-8/

Thank you!