Jump to content

Display total tax for each product tax rule


RomsWebDesign

Recommended Posts

my understanding is that in a shopping cart, you have different products with different tax rate?

 

For invoice, it has been implemented in the way you wanted.

 

I do not think you can do that with default PrestaShop for shopping cart summary.

you may have to customize the shopping cart summary page to implement this way.

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

I think you will have to make changes to the controller file of shopping cart summary page.

 

/classes/Cart.php

/controllers/front/Controller.php

 

or (recommended)

 

/override/class/Cart.php

/override/controllers/front/CartController.pphp

 

You can the

- using $cart->getProducts() to get all products from shopping cart

- and find out the tax rate for each product and group them together.

- and then calculate total tax amount for each group.

- and then assign the calculated result to Smarty variable 

- so that you can use the smarty variables in theme file of shopping cart

 

/themes/shopping-cart.tpl

Link to comment
Share on other sites

  • 1 year later...

/override/class/Cart.php

 

using $cart->getProducts() to get all products from shopping cart and find out the tax rate for each product and group them together.

Add in Cart.php

 

$products = $this->getProducts();
    $virtual_context = Context::getContext()->cloneContext();
 
$address_factory    = Adapter_ServiceLocator::get('Adapter_AddressFactory');
$price_calculator    = Adapter_ServiceLocator::get('Adapter_ProductPriceCalculator');
$configuration        = Adapter_ServiceLocator::get('Core_Business_ConfigurationInterface');
 
$ps_tax_address_type = $configuration->get('PS_TAX_ADDRESS_TYPE');
$ps_use_ecotax = $configuration->get('PS_USE_ECOTAX');
$ps_round_type = $configuration->get('PS_ROUND_TYPE');
$ps_ecotax_tax_rules_group_id = $configuration->get('PS_ECOTAX_TAX_RULES_GROUP_ID');
$compute_precision = $configuration->get('_PS_PRICE_COMPUTE_PRECISION_');
 
$gc_tax_calculation=array();
 
if(!empty($products))
{
 $i=1;
 foreach ($products as $product) {
if ($virtual_context->shop->id != $product['id_shop']) {
$virtual_context->shop = new Shop((int)$product['id_shop']);
}
$ps_tax_address_type = $configuration->get('PS_TAX_ADDRESS_TYPE');
if ($ps_tax_address_type == 'id_address_invoice') {
$id_address = (int)$this->id_address_invoice;
} else {
$id_address = (int)$product['id_address_delivery'];
 
 
if (!$address_factory->addressExists($id_address)) {
$id_address = null;
}
 
            $null = null;
       
 
   
            $org_tax_price = $price_calculator->getProductPrice(
                (int)$product['id_product'],
                false,
                (int)$product['id_product_attribute'],
                6,
                null,
                false,
                true,
                $product['cart_quantity'],
                false,
                (int)$this->id_customer ? (int)$this->id_customer : null,
                (int)$this->id,
                $id_address,
                $null,
                $ps_use_ecotax,
                true,
                $virtual_context
            );
           
 
$address = $address_factory->findOrCreate($id_address, true);
 
$id_tax_rules_group = Product::getIdTaxRulesGroupByIdProduct((int)$product['id_product'], $virtual_context);
 
$tax_calculator = TaxManagerFactory::getManager($address, $id_tax_rules_group)->getTaxCalculator();
 
if(!empty($id_tax_rules_group))
{
 
$tax_rules_name_get='SELECT * FROM  `'._DB_PREFIX_.'tax_rules_group` 
 WHERE id_tax_rules_group = '.$id_tax_rules_group;
$tax_rules_name_get_execute=Db::getInstance()->ExecuteS($tax_rules_name_get);
if(!empty($tax_rules_name_get_execute))
{
  $rule_name=$tax_rules_name_get_execute[0]['name'];
}
 
 
$ff_nal=$tax_calculator->addTaxes($org_tax_price);
 
 
switch ($ps_round_type) {
case Order::ROUND_TOTAL:
$org_tax_price = $org_tax_price * (int)$product['cart_quantity'];
$ff_nal        = $ff_nal * (int)$product['cart_quantity'];
break;
 
case Order::ROUND_LINE:
$org_tax_price = Tools::ps_round($org_tax_price * $product['cart_quantity'], $compute_precision);
$ff_nal = Tools::ps_round($ff_nal * $product['cart_quantity'], $compute_precision);
break;
 
case Order::ROUND_ITEM:
default:
 
$org_tax_price = Tools::ps_round($org_tax_price, $compute_precision) * (int)$product['cart_quantity'];
$ff_nal = Tools::ps_round($ff_nal, $compute_precision) * (int)$product['cart_quantity'];
break;
}
 
if($org_tax_price != $ff_nal)
{
  $product_tax = $ff_nal - $org_tax_price;
  $gc_tax_calculation['tax'.$product['id_product'].'_'.$i]=array('rule'=>$rule_name,'tax'=>$product_tax,
  'id_product'=>$product['id_product'],'grp_id'=>$id_tax_rules_group);
}
}
 
$i++;
 }
 
}
 
$final_tax_logic=array();
if(!empty($gc_tax_calculation))
{
   foreach($gc_tax_calculation as $tax_val)
{
  $grp_id=$tax_val['grp_id'];
  $rule=$tax_val['rule'];
  $tax=$tax_val['tax'];
  $id_product=$tax_val['id_product'];
  if(!isset($final_tax_logic['grp_'.$grp_id]))
  {
    $final_tax_logic['grp_'.$grp_id]=array('rule'=>$rule,'tax'=>$tax,'id_product'=>$id_product,'grp_id'=>$grp_id);
  }
  else
  {
   $old_tax=$final_tax_logic['grp_'.$grp_id]['tax'];
$new_tax=$old_tax + $tax;
$final_tax_logic['grp_'.$grp_id]['tax'] = $new_tax;
  }
}
}

 Assign the calculated result to Smarty variable

 $summary = array(
 'gc_tax_calculation' => $final_tax_logic,
 );
 
 Get calculated result to Smarty variable in shopping-cart.tpl
 
 
{if isset($gc_tax_calculation) && $gc_tax_calculation}                
{foreach from=$gc_tax_calculation item=tax_calculation}
 
<tr class="cart_tax_calculation grp_{$tax_calculation['grp_id']}" id="grp_{$tax_calculation['grp_id']}">
<td colspan="{$col_span_subtotal}" class="text-right">{$tax_calculation['rule']}</td>
<td colspan="2" class="price">{displayPrice price=$tax_calculation['tax']}</td>
</tr>
 
{/foreach}
{/if}

have fun  :D
Edited by kishanunjia (see edit history)
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...