Created
July 12, 2017 14:52
-
-
Save 0-Sony/8781a3d7eea984814cf9c4d615665916 to your computer and use it in GitHub Desktop.
Magento : Create Tax Rules and Tax Class Programmatically with installer
This file contains 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
<?php | |
/** | |
* This file is part of Namespace_Tax for Magento. | |
* | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
* @author Phuong LE <[email protected]> <AgenceSoon> | |
* @category Namespace | |
* @package Namespace_Tax | |
* @copyright Copyright (c) 2017 Agence-Soon (http://www.agence-soon.fr) | |
*/ | |
try { | |
/** @var Mage_Tax_Model_Class $taxClassModel */ | |
$taxClassModel = Mage::getModel('tax/class'); | |
$taxClassModel->setClassName('Professionnels'); | |
$taxClassModel->setClassType(Mage_Tax_Model_Class::TAX_CLASS_TYPE_CUSTOMER); | |
$taxClassModel->save(); | |
} catch (Exception $e) { | |
// Silence is golden | |
Mage::logException($e); | |
if (Mage::getIsDeveloperMode()) { | |
Mage::throwException($e->getMessage()); | |
} | |
} |
This file contains 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
<?php | |
/** | |
* This file is part of Namespace_Tax for Magento. | |
* | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
* @author Phuong LE <[email protected]> <AgenceSoon> | |
* @category Namespace | |
* @package Namespace_Tax | |
* @copyright Copyright (c) 2017 Agence-Soon (http://www.agence-soon.fr) | |
*/ | |
try { | |
/** @var Mage_Tax_Model_Calculation_Rate $taxCalculationRateModel */ | |
$taxCalculationRateModel = Mage::getModel('tax/calculation_rate'); | |
$taxRate20 = $taxCalculationRateModel->loadByCode('FR - 20%'); | |
/** | |
* List our rules code | |
*/ | |
$rulesCode = [ | |
"Professionnels - CE - 20% - 20", | |
"Professionnels - CE - 20% - 10", | |
"Professionnels - CE - 20% - 5.5" | |
]; | |
/** | |
* List our Tax Class Name Type Product | |
* You must create your tax class first | |
*/ | |
$taxClassNameTypeProduct = [ | |
'Biens et services soumis à la TVA à 20%', | |
'Biens et services soumis à la TVA à 10%', | |
'Biens et services soumis à la TVA à 5,5%' | |
]; | |
/** @var Mage_Tax_Model_Class $taxClassModel */ | |
$taxClassModel = Mage::getModel('tax/class'); | |
$model= clone($taxClassModel); | |
/** Here you need to create your "Professionnels" Tax Class before */ | |
$model->load('Professionnels', 'class_name'); | |
$proClassNameId = $model->getId(); | |
$classNameId = []; | |
foreach ($taxClassNameTypeProduct as $name) { | |
$model = clone($taxClassModel); | |
$model->load($name, 'class_name'); | |
$classNameId[] = $model->getId(); | |
} | |
/** @var Mage_Tax_Model_Calculation_Rule $taxRuleModel */ | |
$taxRuleModel = Mage::getModel('tax/calculation_rule'); | |
$i = 0; | |
foreach ($rulesCode as $code) { | |
$model = clone($taxRuleModel); | |
$data = [ | |
'code' => $code, | |
'tax_rate' => [$taxRate20->getId()], | |
'tax_customer_class' => [$proClassNameId], | |
'tax_product_class' => [$classNameId[$i]], | |
'priority' => "0", | |
'position' => "0" | |
]; | |
$model->setData($data); | |
$model->save(); | |
$i++; | |
} | |
/** | |
* Result : Here we will apply a tax rate 20% for each product if the customer is a Professional | |
*/ | |
} catch (Exception $e) { | |
// Silence is golden | |
Mage::logException($e); | |
if (Mage::getIsDeveloperMode()) { | |
Mage::throwException($e->getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment