Jump to content

Add tax to checkout price


Recommended Posts

Hooks are not available for everything.
The difference is also where you want to display prices.
The prices of individual products or the total price of orders are displayed separately.
In your case, add information, paint yours idea.

https://devdocs.prestashop.com/1.7/modules/concepts/hooks/list-of-hooks/

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

Thanks for answer, knacky

Ok, my idea is CHANGE the final price to pay using the tax percentage that belongs to the authenticated user in ALL products to buy

And shows the final price (with this custom tax included) in three views: the cart, fron-office checkout and back-office order

I have 3 images with the examples of what i need to achieve:

cart_page.png

order_page.png

checkout_page.png

Link to comment
Share on other sites

Thank you, perfect.

Only the Prestashop version is missing 😉

I give an example for PS 1.7.x.x

In the first case, there is no such hook.
You have to create your own hook and add it to the TPL template in the module installation, or create a javascript and paste it via an append.

Check template ./themes/classic/templates/checkout/_partials/cart-summary-totals.tpl

You can do this via javascript like this:

$(document).ready(function(){
	var div = document.createElement("div");
  	div.setAttribute('id', 'my-custom-div');
  	div.setAttribute('name', 'my-custom-div');
  	div.setAttribute('class', 'my-custom-div-class');
  	div.setAttribute('style', "display:block; width:100;");
  
 	var span = document.createElement("span");
  	span.setAttribute('id', 'my-custom-tax-label');
  	span.setAttribute('class', 'my-custom-span-class');
  
  	span.innerHTML = myVariable;
  	div.appendChild(span);
  
  	var referenceNode = document.querySelectorAll('.cart-summary-line, .cart-total');
  
  	div.insertBefore(referenceNode);
});

 

and your module:

public function hookHeader($params)
{
	if ($this->context->controller->php_self == 'cart' || $this->context->controller->php_self == 'order') {
		/* read data in cart */		
		$idCart = $this->context->cart->id;
		$cart = new Cart((int)$idcart);

		/* add you variable to JavaScript */
		/* $type Type enum:
     		- ONLY_PRODUCTS
    		- ONLY_DISCOUNTS
     		- BOTH
     		- BOTH_WITHOUT_SHIPPING
     		- ONLY_SHIPPING
     		- ONLY_WRAPPING
		*/
		Media::addJsDef(array('myVariable' => $cart->getOrderTotal(true, Cart::BOTH)));

		/* your javascript for front */
		$this->context->controller->addJS(_PS_MODULE_DIR_.$this->name.'/views/js/cart.js');
	}

}

 

As for administration, it's more complicated there.

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

Or add new hook to template:

./themes/classic/templates/checkout/_partials/cart-summary-totals.tpl

module:

public function addMyHook()
{
	$lines = array();
	foreach(file(_PS_THEME_DIR_.'templates/checkout/_partials/cart-summary-totals.tpl') as $line)
	{
		if("{block name='cart_summary_total'}" === $line)
		{
			array_push($lines, "{hook h='displayMyCustomTax' cart=$cart.id}");
		}
		array_push($lines, $line);
	}
	file_put_contents(_PS_THEME_DIR_.'templates/checkout/_partials/cart-summary-totals.tpl', $lines);
}

public function removeMyHook()
{
	$readTemplate = file_get_contents(_PS_THEME_DIR_.'templates/checkout/_partials/cart-summary-totals.tpl');
	$replace = str_replace("{hook h='displayMyCustomTax' cart=$cart.id}", '', $readTemplate);
	file_put_contents(_PS_THEME_DIR_.'templates/checkout/_partials/cart-summary-totals.tpl', $replace);
}

public function install() 
{

	if (Shop::isFeatureActive())
	{
		Shop::setContext(Shop::CONTEXT_ALL);
	}
        
	if (!parent::install())
	{
		return false;
	}
        
	$this->registerHook('displayMyCustomTax');
	$this->addMyHook();

	return true;
}

public function uninstall()
{                                 
        
	$this->unregisterHook('displayMyCustomTax');
	$this->removeMyHook();

	if (Shop::isFeatureActive())
	{
		Shop::setContext(Shop::CONTEXT_ALL);
	}
        
	if (!parent::uninstall())
	{
		return false;
	}
  
	return true;
}

public function hookDisplayMyCustomTax($params)
{
	$idCart = $params['cart'];
	....
	....
	....
	
	return 'Hello !';
}

 

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

Now I wondered if you didn't want to do something that is already built into Prestashop.
Look in the administration Shop parameters> Customer settings> Groups (find your group and click to Edit).

Section Price display method.

Link to comment
Share on other sites

I've check that, but im not found any option to apply this custom tax, there is dynamic, the value of this percentage changes every month and i need to query the value before the client pay, in my module

I think the view for the cart prices is in /themes/templates/checkout/_partials/cart-summary-totals.tpl. Is there any clean way to modify that view in the module?

Link to comment
Share on other sites

Well, that will be a bit of a problem.
It is normal for the product to set VAT.
Then the group is either pocoli or banned from displaying the price with or without VAT.
The price for VAT payers is lower by the amount of VAT.
But you want the opposite.

This can be solved by creating your own tax and tax rule. Then your created tax is set for all products. Finally, the tpl template is modified.

Link to comment
Share on other sites

/* cart-summary-totals.tpl */

obrazek.png.31d25c492d556ce5def8e8cdd3807fcf.png

/* cart-detailed-totals.tpl */

obrazek.png.73e4df7672ad7f4d8e97f1e5108db547.png

 

Module:

public function hookDisplayMyCustomTax($params)
    {
        $idCart = Context::getContext()->cart->id;

        $cart = new Cart($idCart);
        $productWithoutTax = $cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
        $productWithTax = $cart->getOrderTotal(false, Cart::ONLY_PRODUCTS);
        $getTaxAverage = ($cart->getAverageProductsTaxRate($productWithoutTax, $productWithTax) * 100);
        $getTax = Cart::getTaxesAverageUsed($idCart);

        $taxCalculate = abs($productWithTax - $productWithoutTax);

        $getPriceTax = Tools::displayPrice($taxCalculate);

        $label = $this->l('Ingresos Brutos');
        $tax = $getTaxAverage.'%';
        $value = $getPriceTax;

        $this->context->smarty->assign(array(
            'custom_tax_label' => $label,
            'custom_tax_tax' => $tax,
            'custom_tax_value' => $value
        ));

        return $this->display(_PS_MODULE_DIR_.$this->name, '/views/templates/front/custom-tax.tpl');

    }

 

Sample module:

ps_customtax.zip

Edited by knacky (see edit history)
  • Thanks 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...