Jump to content

Tax Exempt accounts?


fbas

Recommended Posts

I couldn't find a way to do this without overriding code.

 

1) I copied /classes/Tax.php to /override/classes/Tax.php

2) change class definition (line 28) from "class TaxCore extends ObjectModel" to "class Tax extends TaxCore"

3) modify 2 functions, getCarrierTax() AND getProductTaxRate() , to check if address is linked to a user who is placed in a "Tax Exempt" group

// put this just inside the "if (!empty($id_address))" block
		  // if $id_address points to a "Tax Exempt" customer, return $0 tax
   if (is_numeric($id_address)) {
   $address = new Address((int)($id_address));

   // get customer groups to check for tax exempt group
   $groups = Customer::getGroupsStatic((int)($address->id_customer));
   foreach ($groups as $g) {
 $query = "SELECT name FROM group_lang WHERE id_group=$g AND id_lang=1";
 $groupname = Db::getInstance()->getValue($query);
 if (strtolower(substr($groupname, 0, 10))=="tax exempt") return 0;
   }
   }

 

4) add a group in Back Office named (or at least first 10 characters equal to) "Tax Exempt" in Language 1 (English)*

5) add qualifying customers to Tax Exempt group

 

*note, I hard coded the language to id_lang=1 in the query in the code above. I don't know enough about prestashop to make this more configurable.

 

** I don't know anything about other taxes (VAT, ecotax) so I didn't change anything there, but you might need to change it for your situation and country. My situation is simple. I only have 1 category of Tax Exempt - they either pay (state) taxes or not. My shop doesn't charge any other taxes (country, etc) so I could safely ignore that.

Link to comment
Share on other sites

  • 1 month later...
  • 2 weeks later...

I've upgraded my server (previously Prestashop 1.4.3) to 1.4.7 and it's still working for me.

 

did you get any noticeable error messages? was your cache cleared/disabled?

 

you might also need to make the "Tax Exempt" group the default group for those users.

Link to comment
Share on other sites

  • 1 month later...
  • 6 months later...

Where do we insert the code below?

Can you post the entire Modify Tax.php? I really need to get this working. Thank you

 

// put this just inside the "if (!empty($id_address))" block

// if $id_address points to a "Tax Exempt" customer, return $0 tax

if (is_numeric($id_address)) {

$address = new Address((int)($id_address));

 

// get customer groups to check for tax exempt group

$groups = Customer::getGroupsStatic((int)($address->id_customer));

foreach ($groups as $g) {

$query = "SELECT name FROM group_lang WHERE id_group=$g AND id_lang=1";

$groupname = Db::getInstance()->getValue($query);

if (strtolower(substr($groupname, 0, 10))=="tax exempt") return 0;

}

}

Link to comment
Share on other sites

  • 3 months later...
  • 3 weeks later...
  • 3 months later...

I am on prestashop 1.4.9 but you can try the solution I modified for making tax exempt customer groups... I know I wasn't supposed to modify the /classes/Tax.php file but when i created a copy in override of Tax.php I had server errors. So i just went ahead and modified the original /classes/Tax.php file anyways.

 

I have two customer groups (13 and 19) that I want to be tax exempt so I went into Prestashop back office to locate their group id number. After I located it, I applied it to these "if statements", you can modify as necessary or add more if statements for more groups.The non bold/red is original code that is in Tax.php its around line 294 in the file. Be warned though that these changes will be erased if you decide to upgrade Prestashop from your current version.

 

These were my changes in bold/red:

 

if (!empty($id_address))

{

if (is_numeric($id_address)) {

$address = new Address((int)($id_address));

// get customer groups to check for tax exempt group

$groups = Customer::getGroupsStatic((int)($address->id_customer));

foreach ($groups as $g) {

if ($g=13) return 0;

if ($g=19) return 0;

}

}

$address_infos = Address::getCountryAndState($id_address);

if ($address_infos['id_country'])

{

$id_country = (int)($address_infos['id_country']);

$id_state = (int)$address_infos['id_state'];

$id_county = (int)County::getIdCountyByZipCode($address_infos['id_state'], $address_infos['postcode']);

}

 

if (!empty($address_infos['vat_number']) && $address_infos['id_country'] != Configuration::get('VATNUMBER_COUNTRY') && Configuration::get('VATNUMBER_MANAGEMENT'))

return 0;

}

 

 

Hope this helps someone...

  • Like 1
Link to comment
Share on other sites

quik solution for 1.5.x :

 

in the file classes/tax/Tax.php find:

 

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
{
if ($context == null)
$context = Context::getContext();

$address = Address::initialize($id_address);
$id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);

$tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
$tax_calculator = $tax_manager->getTaxCalculator();

return $tax_calculator->getTotalRate();
}

 

and replace with

 

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
{
 if ($context == null)
  $context = Context::getContext();
$address = Address::initialize($id_address);
$groups = Customer::getGroupsStatic((int)($address->id_customer));
$taxratenew = null;
foreach ($groups as $g) {
if ($g > 4) {           //set the group you want here ( '>;' or '=') 
$taxratenew = 16; //set the tax rate you want here (e.g. "0")
[spam-filter]
 if (!$taxratenew) {
 $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);
 $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
 $tax_calculator = $tax_manager->getTaxCalculator();
 $taxratenew = $tax_calculator->getTotalRate();
}
 return $taxratenew;
}

 

Remember to save the file in override/classes/tax/Tax.php and clear cache!

Edited by Pavlos (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 2 months later...
  • 2 weeks later...
  • 2 weeks later...

quik solution for 1.5.x :

 

in the file classes/tax/Tax.php find:

 

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
{
if ($context == null)
$context = Context::getContext();

$address = Address::initialize($id_address);
$id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);

$tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
$tax_calculator = $tax_manager->getTaxCalculator();

return $tax_calculator->getTotalRate();
}
and replace with

 

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
{
  if ($context == null)
   $context = Context::getContext();
$address = Address::initialize($id_address);
$groups = Customer::getGroupsStatic((int)($address->id_customer));
$taxratenew = null;
foreach ($groups as $g) {
if ($g > 4) {           //set the group you want here ( '>;' or '=') 
$taxratenew = 16; //set the tax rate you want here (e.g. "0")
[spam-filter]
  if (!$taxratenew) {
  $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);
  $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
  $tax_calculator = $tax_manager->getTaxCalculator();
  $taxratenew = $tax_calculator->getTotalRate();
}
  return $taxratenew;
}
Remember to save the file in override/classes/tax/Tax.php and clear cache!

 

 

I followed your instructions but It doesn't work for me on 1.5.6.0

Link to comment
Share on other sites

  • 2 months later...
  • 8 months later...

I know this is an old post, but I wanted to confirm that I tried this on 1.5.6.2 and it works just fine.  Great fix!  Just remember not to overwrite it during upgrades.

 

quik solution for 1.5.x :

in the file classes/tax/Tax.php find:
 

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
{
if ($context == null)
$context = Context::getContext();

$address = Address::initialize($id_address);
$id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);

$tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
$tax_calculator = $tax_manager->getTaxCalculator();

return $tax_calculator->getTotalRate();
}
and replace with

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
{
  if ($context == null)
   $context = Context::getContext();
$address = Address::initialize($id_address);
$groups = Customer::getGroupsStatic((int)($address->id_customer));
$taxratenew = null;
foreach ($groups as $g) {
if ($g > 4) {           //set the group you want here ( '>;' or '=') 
$taxratenew = 16; //set the tax rate you want here (e.g. "0")
[spam-filter]
  if (!$taxratenew) {
  $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);
  $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
  $tax_calculator = $tax_manager->getTaxCalculator();
  $taxratenew = $tax_calculator->getTotalRate();
}
  return $taxratenew;
}
Remember to save the file in override/classes/tax/Tax.php and clear cache!

 

Link to comment
Share on other sites

  • 2 months later...
  • 5 months later...

I have tried to do put this code into override/classes/tax/Taxes.php

 

 

 

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
{
if ($context == null)
$context = Context::getContext();
$address = Address::initialize($id_address);
$groups = Customer::getGroupsStatic((int)($address->id_customer));
$taxratenew = null;
foreach ($groups as $g) {
if ($g > 4) { //set the group you want here ( '>;' or '=')
$taxratenew = 16; //set the tax rate you want here (e.g. "0")
[spam-filter]
if (!$taxratenew) {
$id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);
$tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
$tax_calculator = $tax_manager->getTaxCalculator();
$taxratenew = $tax_calculator->getTotalRate();
}
return $taxratenew;
}

 

 

 

However, no matter how much I clear cache I can not get it to generate order without tax for a Tax Exempt distributor. Any thoughts? Using Prestashop 1.6.0.14

 

Thank you,

  • Like 1
Link to comment
Share on other sites

  • 5 months later...

Hi all, If tried in prestashop 1.6.0.9 and it doesn't work even if I do the following:
 

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
{
   if ($context == null)
      $context = Context::getContext();
   $address = Address::initialize($id_address);
   $groups = Customer::getGroupsStatic((int)($address->id_customer));
   $taxratenew = null;
   foreach ($groups as $g) {
      if ($g > 4) { //set the group you want here ( '>;' or '=') 
         $taxratenew = 16; //set the tax rate you want here (e.g. "0")
      [spam-filter]
   if (!$taxratenew) {
      $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);
      $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
      $tax_calculator = $tax_manager->getTaxCalculator();
      $taxratenew = $tax_calculator->getTotalRate();
   }
   return 16; //hard coded 16 just to si if something changes in my override file and also in the core file
}

after doing this I get the same tax as I had allways...

 

Anybody knows why ?

 

 

thanks in advance

Edited by Christian Alias (see edit history)
Link to comment
Share on other sites

  • 5 months later...
 public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
    {
        if ($context == null)
            $context = Context::getContext();
        $address = Address::initialize($id_address);
        $groups = Customer::getGroupsStatic((int)($address->id_customer));
        $taxratenew = null;
        foreach ($groups as $g) {
            if ($g == 8 || $g == 9) {           //set the group you want here ( '>;' or '=')
                return 0; //set the tax rate you want here (e.g. "0")
            }
        }
        if (!$taxratenew) {
            $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);
            $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
            $tax_calculator = $tax_manager->getTaxCalculator();
            $taxratenew = $tax_calculator->getTotalRate();
        }
        return $taxratenew;
    }

Try this, it works with tax = 0;

Edited by piotr94 (see edit history)
Link to comment
Share on other sites

  • 1 month later...
 public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
    {
        if ($context == null)
            $context = Context::getContext();
        $address = Address::initialize($id_address);
        $groups = Customer::getGroupsStatic((int)($address->id_customer));
        $taxratenew = null;
        foreach ($groups as $g) {
            if ($g == 8 || $g == 9) {           //set the group you want here ( '>;' or '=')
                return 0; //set the tax rate you want here (e.g. "0")
            }
        }
        if (!$taxratenew) {
            $id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);
            $tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
            $tax_calculator = $tax_manager->getTaxCalculator();
            $taxratenew = $tax_calculator->getTotalRate();
        }
        return $taxratenew;
    }

Try this, it works with tax = 0;

 

Would you explain this somewhat more? I have replaced your function with the 'regular' getProductTaxRate function in classes/tax/Tax.php. But is does not work, I have only changed the groups numbers from '8' and '9' to '4'

 

Btw: Wha do you mean with: it works with tax = 0; ?

Edited by Zadara (see edit history)
Link to comment
Share on other sites

  • 1 month later...

Hi, today i had the same Problem.. I have ONE UserGroup that should not have any TAX (not in Product-List and not in Cart).

I tried the suggestions above but had no success...

 

I found the Class TaxCalculatorCore in /classes/tax/TaxCalculator.php and modified the Method getTaxesTotalAmount($price_te)

/**
* Return the total taxes amount
*
* @param float $price_te
* @return float $amount
*/
public function getTaxesTotalAmount($price_te)
{
	$amount = 0;

	$taxes = $this->getTaxesAmount($price_te);
	foreach ($taxes as $tax)
	$amount += $tax;

	$context = Context::getContext();
	if($context->customer->id_default_group == 17 ) // 17 is the UserGroup whith no tax
		return 0;
	else
		return $amount;
}

Seems also to work on on invoice and E-Mails

 

tested: Prestashop 1.6.0.14

 

 

SRY: I have no idea how to override this.. maybe someone could to that and provide the file?!

Link to comment
Share on other sites

Hi, today i had the same Problem.. I have ONE UserGroup that should not have any TAX (not in Product-List and not in Cart).

I tried the suggestions above but had no success...

 

I found the Class TaxCalculatorCore in /classes/tax/TaxCalculator.php and modified the Method getTaxesTotalAmount($price_te)

/**
* Return the total taxes amount
*
* @param float $price_te
* @return float $amount
*/
public function getTaxesTotalAmount($price_te)
{
	$amount = 0;

	$taxes = $this->getTaxesAmount($price_te);
	foreach ($taxes as $tax)
	$amount += $tax;

	$context = Context::getContext();
	if($context->customer->id_default_group == 17 ) // 17 is the UserGroup whith no tax
		return 0;
	else
		return $amount;
}

Seems also to work on on invoice and E-Mails

 

tested: Prestashop 1.6.0.14

 

 

SRY: I have no idea how to override this.. maybe someone could to that and provide the file?!

Hello!

 

you have to create this directory:

override/classes/tax/TaxCalculator.php

 

and TaxCalculator.php should be:

<?php

class TaxCalculator extends TaxCalculatorCore
{
public function getTaxesTotalAmount($price_te)
{
    $amount = 0;

    $taxes = $this->getTaxesAmount($price_te);
    foreach ($taxes as $tax)
    $amount += $tax;

    $context = Context::getContext();
    if($context->customer->id_default_group == 17 ) // 17 is the UserGroup whith no tax
        return 0;
    else
        return $amount;
}
}

I'm not 100% shure because I haven't tried but that should work, now leave the original file in classes/tax/TaxCalculator.php like it was before you modified

 

good luck!

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Hi all

I wonder what I did wrong, those several methods does not works ...

I tried the last one

I duplicated TaxCalculator.php

in Override/classes/tax

erase inside and replace with this

 

<?php

class TaxCalculator extends TaxCalculatorCore
{
public function getTaxesTotalAmount($price_te)
{
    $amount = 0;

    $taxes = $this->getTaxesAmount($price_te);
    foreach ($taxes as $tax)
    $amount += $tax;

    $context = Context::getContext();
    if($context->customer->id_default_group == 4 ) // 4 is my UserGroup whith no tax
        return 0;
    else
        return $amount;
}
}

Edited by Pr0t3us (see edit history)
Link to comment
Share on other sites

When I set $taxratenew to "0" It doesn't work for me. When I set it to "5" or something it works. 

 

Anyone solution?

Using 1.5.6.1

I know this is an old comment but for any looking for a solution here, you can try

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
	{
		if ($context == null){
			$context = Context::getContext();
		}
		$address = Address::initialize($id_address);
		$groups = Customer::getGroupsStatic((int)($address->id_customer));
		$taxratenew;
		foreach ($groups as $g){
			if ($g == 5){           //set the group you want here ( '>;' or '=') 
				$taxratenew = 0;
			}
		}
		if(!isset($taxratenew)){
				$id_tax_rules = (int)Product::getIdTaxRulesGroupByIdProduct($id_product, $context);
				$tax_manager = TaxManagerFactory::getManager($address, $id_tax_rules);
				$tax_calculator = $tax_manager->getTaxCalculator();
				$taxratenew = $tax_calculator->getTotalRate();
		}
		return $taxratenew;
	}
}

File : /classes/tax/Tax.php

Do not forget to override it in override/classes/tax/Tax.php

The solution works in 1.6.0.x

Link to comment
Share on other sites

Thanks to everyone on this thread who has worked to hack a feature into Prestashop that should be part of its default core options :)

But, I have tried everything in this thread (some of which is conflicting) and can not get it to work!

 

Someone please write a one post only set of instructions on what files to change and where to make one group tax exempt?

 

( I am running Prestashop 1.6.1.6 )

Thank You All,
 

Aaron

rhizOHM.co

Edited by aaronsuncamacho (see edit history)
Link to comment
Share on other sites

  • 2 months later...

Did anyone find a solution for 1.6.1.17?  I tried adding that code to override/classes/tax/TaxCalculator.php and it does NOT work for me.

 

 

override/classes/tax/TaxCalculator.php reads:

<?php

class TaxCalculator extends TaxCalculatorCore
{
public function getTaxesTotalAmount($price_te)
{
    $amount = 0;

    $taxes = $this->getTaxesAmount($price_te);
    foreach ($taxes as $tax)
    $amount += $tax;

    $context = Context::getContext();
    if($context->customer->id_default_group == 46 ) // 17 is the UserGroup whith no tax
        return 0;
    else
        return $amount;
}
}

Am I missing anything? Do i need any other code to change in any of the other PHP files?

Edited by televi (see edit history)
Link to comment
Share on other sites

  • 6 months later...

PRESTASHOP DEVELOPERS!!    This is such an obviously needed function that should be part of the store without any hacking, or addon modules.  And if not, then a Prestashop Developer should reply to this thread and help all of us who need this.  I'm so frustrated.  :angry:

Link to comment
Share on other sites

For anybody else who finds themselves here, the older solutions don't work on 1.6.1.9, this does:

  1. Create the customer group that you want to tax-exempt and get its ID (it's displayed in the admin panel view of customer groups).
  2. Copy "[prestashop root]/classes/tax/TaxRulesTaxManager.php" to "[prestashop root]/override/classes/tax/TaxRulesTaxManager.php"
  3. In "[prestashop root]/override/classes/tax/TaxRulesTaxManager.php", in function "getTaxCalculator", replace "$tax_enabled = $this->configurationManager->get('PS_TAX');" with "$tax_enabled = $this->configurationManager->get('PS_TAX') && !in_array(GROUP_ID, Customer::getGroupsStatic($this->address->id_customer));", replacing GROUP_ID with the group ID we found earlier.
  4. Delete "[prestashop root]/cache/class_index.php" so PrestaShop looks for then uses your override, rather than the unmodified original TaxRulesTaxManager.php file.

If you're reading this long after I post it, and the solution obsoleted again, I recommend approaching the problem by looking for everywhere the PS_TAX configuration option is consulted and, where appropriate, creating an override that also checks the customer group ID.

 

PrestaShop devs, please implement this natively, it will save many of us time and frustration, and reduce bugs in the average PrestaShop install.

  • Like 1
Link to comment
Share on other sites

@El Patron.  Yes, I know about that addon, which to me is $45.  I appreciate you posting that for people who don't have a developer on staff. I just wish the prestashop developers were more helpful in contributing to the forum, AND that a tax exempt group was already available in the standard store. 

 

We switched from OpenCart to Prestashop for various reasons of functionality, but had I known the forum wasn't being assisted by PS Devs, I might not have switched. When I had questions for the OpenCart community, they were answered in minutes or hours, and sometimes a day or 2. But in PS forums I see things going for weeks and months and sometimes totally unanswered for years.  It's very disappointing. 

Link to comment
Share on other sites

@El Patron.  Yes, I know about that addon, which to me is $45.  I appreciate you posting that for people who don't have a developer on staff. I just wish the prestashop developers were more helpful in contributing to the forum, AND that a tax exempt group was already available in the standard store. 

 

We switched from OpenCart to Prestashop for various reasons of functionality, but had I known the forum wasn't being assisted by PS Devs, I might not have switched. When I had questions for the OpenCart community, they were answered in minutes or hours, and sometimes a day or 2. But in PS forums I see things going for weeks and months and sometimes totally unanswered for years.  It's very disappointing. 

 

Yes, I see value of this being a native feature.  The only way you can get that on roadmap is to open a feature request here: http://forge.prestashop.com

 

This is the only place you can be heard by PS.  The forum is pretty much entirely community.

Link to comment
Share on other sites

  • 2 years later...
On 5/23/2017 at 2:40 PM, twhb said:

For anybody else who finds themselves here, the older solutions don't work on 1.6.1.9, this does:

  1. Create the customer group that you want to tax-exempt and get its ID (it's displayed in the admin panel view of customer groups).
  2. Copy "[prestashop root]/classes/tax/TaxRulesTaxManager.php" to "[prestashop root]/override/classes/tax/TaxRulesTaxManager.php"
  3. In "[prestashop root]/override/classes/tax/TaxRulesTaxManager.php", in function "getTaxCalculator", replace "$tax_enabled = $this->configurationManager->get('PS_TAX');" with "$tax_enabled = $this->configurationManager->get('PS_TAX') && !in_array(GROUP_ID, Customer::getGroupsStatic($this->address->id_customer));", replacing GROUP_ID with the group ID we found earlier.
  4. Delete "[prestashop root]/cache/class_index.php" so PrestaShop looks for then uses your override, rather than the unmodified original TaxRulesTaxManager.php file.

If you're reading this long after I post it, and the solution obsoleted again, I recommend approaching the problem by looking for everywhere the PS_TAX configuration option is consulted and, where appropriate, creating an override that also checks the customer group ID.

 

PrestaShop devs, please implement this natively, it will save many of us time and frustration, and reduce bugs in the average PrestaShop install.

This way works for newest version 1.7.6.0 as well, and free. Thanks

  • Like 1
Link to comment
Share on other sites

  • 7 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...