Magento – Magento: How to set VAT attribute

databasemagento-1.9MySQLPHPtax-class

I am new to this magento world and stuck on a problem: I need to create a attribute rule which can add 20% in all prices by default. In fact I created a rule with name "standard VAT" in Sales->Tax->Manage Tax Rules.

However, by default Magento is using "None" as the default Tax Class in Product > Price > Tax Class. How can we make Magento use my tax class "Standard VAT" by default instead?

I know one way is by selecting all products and editing the Tax Class attribute, but that is not a viable solution for us. If we go this route we have to apply that edit too often as our listing updates every 30 minute.

We need a 1 time solution so that all our newly add products and existing products can automatically use the new Tax Class.

If you know any way we can edit any file in magento please advise us with full file path, code to add and line number.

Thank you for your help in advance.

(I did what ever they recommenced here but this inst the solution for me: magentocommerce.com/knowledge-base/entry/ce18-and-ee113-vat-gst)

Best Answer

Unfortunately Magento does not have a setting in the Admin to force every new product created to default to a specified Tax Class. Without knowing your specific product loading procedure I am going to give you the most general, reusable solution for defaulting the Tax Class of new products.

First you need the Id of you new tax class. In the admin go to "Sales -> Tax -> Product Tax Classes" and click on the row for your "standard Tax" that you created. The URL in the browser window would be something like : http://website.com/index.php/admin/tax_class_product/edit/id/12/key/025f7b6050a3fdd31960637732d3b9f9/ You want the number between edit/id/ and /key/. That is the id of your tax class.

Next steps would be writing code to make a custom Magento module that would update the Tax Class every time you create a new product.

  1. Create a file /app/etc/modules/Stackoverflow_Example.xml and put the following in it. This is a module declaration that is telling Magento you are creating a module and where to find the files for it.

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <Stackoverflow_Example>
                <active>true</active>
                <codePool>local</codePool>
            </Stackoverflow_Example>
        </modules>
    </config>
    
  2. Create a file /app/code/local/Stackoverflow/Example/etc/config.xml and put the following into it. You will notice that some of the fields in the previous file match up to the path of this file and some of the nodes here. This file is telling Magento where to find your new Model Stackoverflow_Example_Model and also is telling Magento whenever you are creating a new product (catalog_product_new_action) to run some custom code on that product <method>catalog_product_new_action</method> and the custom code is found in a model <class>stackoverflow_example/product_observer</class> that Magento will now know how to find because we told it how to find our custom Models higher up.

    <?xml version="1.0"?>
    <config>
        <modules>
            <Stackoverflow_Example>
                <version>1.0.0</version>
            </Stackoverflow_Example>
        </modules>
        <global>
            <models>
                <stackoverflow_example>
                     <class>Stackoverflow_Example_Model</class>
                </stackoverflow_example>
            </models> 
            <events>
                <catalog_product_new_action>
                    <observers>
                      <stackoverflow_example_product_observer>
                        <type>singleton</type>
                        <class>stackoverflow_example/product_observer</class>
                        <method>catalog_product_new_action</method>
                      </stackoverflow_example_product_observer>
                    </observers>
                </catalog_product_new_action>
            </events>
        </global>
    </config>
    
  3. Create a file /app/code/local/Stackoverflow/Example/Model/Product/Observer.php and put the following code in it. All we are doing in this file is creating a simple PHP function that will determine the new Product that is being created $product = $observer->getEvent()->getProduct(); and it is then setting the tax class of it before it is saved $product->setTaxClassId(12);. Of course I am using 12 here because it was what was between edit/id/ and /key/ in my example above. However if your "standard Tax" class was the first one created on a new store it may be 3 or 4.

    <?php
    
    class Stackoverflow_Example_Model_Product_Observer
    {
        public function catalog_product_new_action($observer) {
    
            $product = $observer->getEvent()->getProduct();
            $product->setTaxClassId(12);
        }
    }
    
Related Topic