Last active
May 30, 2019 21:36
-
-
Save Alexander-Pop/114ef387a91be9e09ccb592b60a2d505 to your computer and use it in GitHub Desktop.
Magento - programmatically Creating a Tax Rate, Tax Rule, Editing Tax Rate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Programmatically Creating a Tax Rate | |
(for the state of North Carolina(NC). You can modify it as needed to create a rate: | |
/* Helper Function for Getting a Region ID from a state code(NC) */ | |
function get_region_id_from_state($state){ | |
$region_data = Mage::getModel('directory/region')->loadByCode($state, 'NC'); | |
if(!$region_data || $region_data->getId() <= 0){ | |
return false; | |
} | |
return $region_data->getId(); | |
} | |
function create_tax_rate($rate_name, $state=false, $zipcode=false, $tax_percent){ | |
if(empty($rate_name) || $tax_percent <= 0 || (empty($state) && empty($zipcode))){ | |
return false; | |
} | |
if(!empty($state)){ | |
$region_id = get_region_id_from_state($state); | |
if(empty($region_id)){ | |
return false; | |
} | |
} else{ | |
$region_id = 0; | |
} | |
$tax_rate = Mage::getModel('tax/calculation_rate'); | |
$tax_rate->setCode($rate_name); | |
$tax_rate->setTaxCountryId('US'); | |
if(!empty($zipcode)){ | |
$tax_rate->setTaxRegionId($region_id); | |
$tax_rate->setTaxPostcode($zipcode); | |
} else{ | |
$tax_rate->setTaxRegionId($region_id); | |
$tax_rate->setTaxPostcode('*'); | |
} | |
$tax_rate->setRate($tax_percent); | |
$tax_rate->setZipIsRange(0); | |
$tax_rate->save(); | |
return $tax_rate; | |
} | |
Programmatically Creating a Tax Rate | |
In order to create a tax rule, you need to know the numeric ID of at least one tax rate, along with the numeric ID of a product tax class and customer tax class. You can find these a few ways, for the purpose of this example, we are creating a rule for an existing product tax class. The example would be similar when doing a customer tax class. | |
Note that on your install, you will at minimum need to verify the $default_customer_tax_class and $default_product_tax_class variables are correct. | |
function get_tax_rate_by_name($tax_rate_name){ | |
$tax_rate = $search_results = Mage::getModel('tax/calculation_rate')->load($tax_rate_name, 'code'); | |
return $tax_rate; | |
} | |
function get_tax_rule_by_name($tax_rule_name){ | |
$tax_rule = Mage::getModel('tax/calculation_rule')->load($tax_rule_name, 'code'); | |
return $tax_rule; | |
} | |
function get_tax_class_by_name($tax_class_name){ | |
$tax_class = Mage::getModel('tax/class')->load($tax_class_name, 'class_name'); | |
return $tax_class; | |
} | |
function create_tax_rule($rule_name, $rate_id){ | |
//The customer tax class for a retail customer is 3 for my magento install. You may need to verify this by logging into the backend. | |
$default_customer_tax_class = 3; | |
//The product tax class name. You will need to setup/add a tax class(or use one of the defaults.)backend. | |
$default_product_tax_class = 'my_custom_tax_class_name'; | |
if(!is_array($rate_id)){ | |
$rate_id = intval($rate_id); | |
} | |
if(empty($rule_name) || (!is_array($rate_id) && $rate_id <= 0)){ | |
return false; | |
} | |
$existing_rule = get_tax_rule_by_name($rule_name); | |
if($existing_rule && $existing_rule->getId() > 0){ | |
echo 'A rule with this name already exists'; | |
return false; | |
} | |
$tax_class = get_tax_class_by_name($default_product_tax_class); | |
if(!$tax_class && $tax_class->getId() <= 0){ | |
echo 'This tax class is invalid'; | |
return false; | |
} | |
$new_rule = Mage::getModel('tax/calculation_rule'); | |
$new_rule->setCode($rule_name); | |
$new_rule->setTaxCustomerClass(array($default_customer_tax_class)); | |
$new_rule->setTaxProductClass(array($tax_class->getId())); | |
if(is_array($rate_id)){ | |
$new_rule->setTaxRate($rate_id); | |
}else{ | |
$new_rule->setTaxRate(array($rate_id)); | |
} | |
$new_rule->setPriority(0); | |
$new_rule->setPosition(0); | |
$new_rule->save(); | |
return $new_rule; | |
} | |
Programmatically Editing Tax Rate | |
Here is where I ran into issues initially and noticed the difference between both getData and GetTaxRate between a newly created rule and one loaded via the load function. Specifically, when retrieving the rates, you need to use getRates to get an array of tax rates and the other variables, like getTaxCustomerClass were not returning arrays, but were instead returning strings. | |
The fix ended up being doing the edit in the same manner as the creation. See below: | |
$default_product_tax_class = 'my_custom_tax_class_name'; | |
$default_customer_tax_class = 3; | |
$tax_rate = get_tax_rate_by_name('my_new_tax_rate'); | |
if($tax_rate && $tax_rate->getId() > 0){ | |
echo 'Error getting tax rate.'; | |
return false; | |
} | |
$tax_class = get_tax_class_by_name($default_product_tax_class); | |
if(!$tax_class && $tax_class->getId() <= 0){ | |
echo 'This tax class is invalid'; | |
return false; | |
} | |
$tax_rule = get_tax_rule_by_name($tax_rule_name); | |
if($tax_rule && $tax_rule->getId() > 0){ | |
echo 'Error getting tax rule.'; | |
return false; | |
} | |
$tax_rates = $tax_rule->getRates(); | |
$tax_rates[] = $tax_rate->getId(); | |
$tax_rule->setTaxCustomerClass(array($default_customer_tax_class)); | |
$tax_rule->setTaxProductClass(array($tax_class->getId()); | |
$tax_rule->setTaxRate($tax_rates); | |
$tax_rule->setPriority(0); | |
$tax_rule->setPosition(0); | |
$tax_rule->save(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment