首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >折扣后,如果总计为0,则需要根据自定义价格字段计税,并添加到购物车页面的总计中

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

Stack Overflow用户
提问于 2015-01-16 14:58:43
回答 2查看 1.3K关注 0票数 1

我已经创建了一张“固定金额”的优惠券,假设是100美元,我正在将产品添加到购物车中,价格为100美元。现在我已经在系统->配置->税务->计算设置中选择了后端,“在折扣后申请客户税”和“在价格上申请折扣是不含税的”。

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

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

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

我已经尝试了以下几件事:我正在观察以下观察者:

代码语言:javascript
复制
        <checkout_cart_save_after>
            <observers>
                <NamespaceModulename>
                    <type>singleton</type>
                    <class>Namespace_Modulename_Model_Observer</class>
                    <method>updateTaxAfterDiscountApplied</method>
                </NamespaceModulename>
            </observers>
        </checkout_cart_save_after>

下面是我的方法代码:

代码语言:javascript
复制
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%,那么它应该计算40的12%,并添加到总计中。那么如何获取税收,这样我就可以用tax_calculation_price计算它了。?任何人都会感激的。

EN

回答 2

Stack Overflow用户

发布于 2015-01-16 17:16:22

您可以使用此obeserver事件

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2015-01-28 22:28:39

我终于得到了我的问题的答案。在这里,我给出的代码,以更新税后应用您的优惠券,如果总零,那么它也将更新税收。在全局节点内部的config.xml中编写以下代码。

代码语言:javascript
复制
    <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>

创建了observer.php,并添加了以下方法:

代码语言:javascript
复制
//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 Events的更多信息,请访问以下链接:https://www.nicksays.co.uk/magento-events-cheat-sheet-1-8/

谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27978781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档