Jump to content

[v.1.6.0.6] Different tax rates for different user groups. Is it possible?


Recommended Posts

I need the option to apply different tax rates (22%, 10% and 4%) to the same product, based on which group the customer is into.

 

So for example:

- user from group A will be able to buy all my products with vat at 22%

- user from group B will be able to buy all my products with vat at 10%
- user from group C will be able to buy all my products with vat at 4%

 

Is it possible? How can I achieve this? 

I can't find any addon with this feature, do I have to get my hands dirty with php?

 

Any suggestion on where to look will be appreciated.

 

Thanks!

Stefano

 

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

I've tried to copy the file classes/tax/Tax.php

here override/classes/tax/Tax.php

and then I've editet this function:
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();
} 

into this one

 

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 = 1; //set the tax rate you want here (e.g. "0")
  } else if ($g == 5) {           //set the group you want here ( '>;' or '=') 
   $taxratenew = 2; //set the tax rate you want here (e.g. "0")
  } else if ($g == 6) {           //set the group you want here ( '>;' or '=') 
   $taxratenew = 4; //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 = 22;
 }
 return $taxratenew;
}

The problem is that $g, which contains the returned value of Customer::getGroupsStatic((int)($address->id_customer)); is always 1...

 

What am I doing wrong? What's the proper method to get the user's group id?

 

Thanks!

 

Link to comment
Share on other sites

I fixed the problem, now the working function is this one and each group has its own tax rate (but if I want to edit it I need to edit the code, because the tax rates set in the admin panel are basically ignored)

public static function getProductTaxRate($id_product, $id_address = null, Context $context = null)
	{
		if ($context == null)
			$context = Context::getContext();
							
		$groups = Customer::getGroupsStatic($context->cart->id_customer);
		$taxratenew = null;
		foreach ($groups as $g) {
			switch ($g) {
				case 1:
				case 2:
				case 3:
				case 4:
					$taxratenew = 22;
					break;
				case 5:
					$taxratenew = 10;
					break;
				case 6:
					$taxratenew = 4;
					break;
				default:
					$taxratenew = 10;
			}
		}
		
		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;
	}
  • Like 2
Link to comment
Share on other sites

  • 8 months later...

Hi. First off thank you very much for this code. It works awesome except i noticed that sometimes paypal won't include the wholesale tax. It's funny but it's usually when i have 3 items in the shopping cart. If i have 2 or 4 items paypal will show and include the tax. Any ideas??? 

Link to comment
Share on other sites

  • 3 months later...

Thanks -stefanoginella. That works but Im having a problem: I want my shipping to be taxed and it's still taxed even when I use your code to set the product tax to 0. I changed the function getCarrierTaxRate to emulate it but it looks like that function isn't even used in the tax.php or anywhere else in the code.

 

Any suggestions on how to accomplish this?

Link to comment
Share on other sites

  • 3 weeks later...

I tried this on PS 1.6.0.14, and it was not working for me. For debugging I even set $taxratenew = (float)0; right before the return just to test, and it still calculated tax in the cart! I tried in both override/classes/tax/Tax.php and I tried modifying directly classes/tax/Tax.php just to rule out any override problems. Same thing - customers always have tax.

Link to comment
Share on other sites

  • 4 months later...

I also tried these changes on PS 1.6.1.1 and it is not working as well.  My experience is very similar to the user above.  The changes seem to have no effect on the tax rate charged to the customer.  Anyone else try these changes on 1.6.1.1?

Link to comment
Share on other sites

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...