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


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

我创建了一个带有"固定金额"的优惠券,假设为 100 美元,我正在将价格为 100 美元的产品添加到购物车中。现在,我已经在系统>配置>税>计算设置的后端选择了"应用客户税后折扣"和"应用价格折扣不含税"。

现在下面是场景:1)当我将产品添加到购物车并应用优惠券时,总计变为0。(因为优惠券价值 100 和产品价格为 100,所以它变成了 0)现在如果我按下结帐按钮,在结帐页面价格显示 $0,税也显示 $0。

如果我使用优惠券,我想申请一次税,我的税应该根据 40 美元的金额计算(例如)。 如果我为金额为 10% 的州创建了税收规则,那么我的税款应计算为 $40*10/100 = $4,应在单击结帐按钮之前将税款添加到总额中。

有谁知道我需要在哪个观察者中查找以添加这些条件。

我尝试了以下事情:我观察到以下观察者:

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

以下是我的方法代码:

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'];
        }
    }

}

在上面的方法中,"tax_calculation_price"是我的自定义字段,当我应用优惠券和我的总计零时使用它。 然后我有这个字段,用于根据税收计算计算税款。 例如tax_calculation_price = 40,假设加拿大>安大略省我的税率是 12%,那么它应该计算 12 的 40% 并添加到总计中。 那么如何取税,以便我可以用tax_calculation_price计算它。?任何帮助赞赏。

您可以使用此 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> 

终于我得到了问题的答案。在这里,我给出了在应用优惠券后更新税收的代码,如果总为零,那么它也将更新税收。 在全局节点的 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>

创建了观察者.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;
        }               
    } 
} 

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

谢谢!