【问题标题】:Set product tax in Magento observer在 Magento 观察者中设置产品税
【发布时间】:2014-07-15 09:02:18
【问题描述】:

我正在使用sales_quote_add_item,我想更改产品的税级。 在观察者中,我检索产品:

$event = $observer->getEvent();
$item = $event->getQuoteItem();
$product = $item->getProduct();

我尝试了$item->setTaxClassId()$product->setTaxClassId() 方法,但没有奏效。

还有其他可用的方法吗?

【问题讨论】:

  • 你现在在哪个项目工作?

标签: php magento magento-1.8


【解决方案1】:

如果您想使用observer 更改product_tax_class,则必须使用sales_quote_item_set_product

函数Mage_Sales_Model_Quote_Item::setProduct在每次保存产品时都会重置一些基本信息

config.xml

<config>
...
    <global>
        <events>
            <sales_quote_item_set_product>
                <observers>
                    <samples>
                        <type>singleton</type>
                        <class>samples/observer</class>
                        <method>salesQuoteItemSetProduct</method>
                    </samples>
                </observers>
            </sales_quote_item_set_product>
        </events>
    <global>
...
</config>

Observer.php

class Mynamespace_Samples_Model_Observer
{
    public function salesQuoteItemSetProduct(Varien_Event_Observer $observer)
    {
        /* @var $item Mage_Sales_Model_Quote_Item */
        $item = $observer->getQuoteItem();

        $item->setTaxClassId('2');

        return $this;
    }
}

如果您有任何疑问,请告诉我

【讨论】:

  • 很遗憾,这不会更改添加到购物车的产品的税级。
  • 我在 sales_quote_item_set_product 活动中尝试过,正如您在回答中解释的那样。
  • 如果您觉得有帮助,请接受答案@GerarddeVisser
  • @KeyurShah,如何在此处设置自定义税额而不是税号?对于具体的报价项目
【解决方案2】:

而不是$item-&gt;setTaxClassId()$product-&gt;setTaxClassId()
试试

$item-&gt;getProduct()-&gt;setTaxClassId()

【讨论】:

  • 我已经尝试过了,正如您在我的问题中看到的那样,但它没有用。
  • 抱歉错过了!你在观察者 sales_quote_collect_totals_before 中尝试过吗?
  • 也可以试试代码$cartItems = $quote-&gt;getAllVisibleItems(); foreach ($cartItems as $item) { $item-&gt;getProduct()-&gt;setTaxClassId(); }
  • @John,如何设置报价项目的固定税额?
  • @jafarpinjar 对不起!不知道,自从我从事 Magento 工作以来已经有一段时间了!
【解决方案3】:

我已经解决了。

我正在开发 GST 模块,因为印度改变了我们的税收制度。

转到您的自定义模块并在 config.xml 中添加以下事件观察器

<events>    
   <sales_quote_collect_totals_before>
       <observers>
           <sales_quote_add_item_handler>
               <type>model</type>
               <class>gst/observer</class>
               <method>checkTax</method>
           </sales_quote_add_item_handler>
       </observers>
   </sales_quote_collect_totals_before>
</events>

现在在你模块的 Model 文件夹中创建 Observer.php 并在其中添加以下函数。

public function checkTax(Varien_Event_Observer $observer) {
    $quote = $observer->getQuote();
    foreach ($quote->getAllVisibleItems() as $quote_item) {
        $product = $quote_item->getProduct();
        $product->setTaxClassId(2);
        $quote_item->setPriceInclTax($quote_item->getPrice());
        $quote_item->setBasePriceInclTax($quote_item->getBasePrice());
        $quote_item->setBaseRowTotalInclTax($quote_item->setBaseRowTotal());
    }
    $quote->save();
}

希望对你有所帮助:)

【讨论】:

    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多