简体   繁体   English

折扣后,如果总计为0,则需要根据自定义价格字段计算税款,并将其添加到购物车页面的总计

[英]after discount if grand total 0 then need calculate tax based on custom price field and add into grand total in cart page

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. 我用“固定金额”创建了一张优惠券,假设该优惠券的金额为100美元,我正在向购物车中添加价格为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. 现在是下面的场景:1)当我将产品添加到购物车并应用优惠券时,总计变为0。(因为优惠券价值为100,产品价格为100,因此它变为0)现在如果我在结帐页面上按结帐按钮,显示$ 0,税收也显示$ 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). 如果我申请优惠券,我想申请一次税,我的税应根据40美元(例如)的某个金额计算得出。 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. 如果我已经为州创建了金额为10%的税收规则,那么我的税金应按$ 40 * 10/100 = $ 4计税,应在单击结帐按钮之前将税金加到总计中。

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. 在上面的方法中,“ tax_calculation_price”是我的自定义字段,当我应用优惠券和总计为零时使用。 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. 例如tax_calculation_price = 40然后,假设加拿大->安大略省,我的税率为12%,则应计算40的12%并加到总计中。 so how to fetch tax so that I can calculate it with tax_calculation_price. 因此如何提取税款,以便我可以使用tax_calculation_price进行计算。 ? any help appreciate. 任何帮助赞赏。

You can use this obeserver event 您可以使用此obeserver事件

<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> 

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. 在全局节点内的config.xml中编写以下代码。

    <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: 创建了observer.php,并添加了以下方法:

//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/ 有关Magento活动备忘单的更多信息,请点击以下链接: https : //www.nicksays.co.uk/magento-events-cheat-sheet-1-8/

Thank you! 谢谢!

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

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