Nicolas Pistillo Posted April 6, 2022 Share Posted April 6, 2022 Hi! i'm creating a module for add a tax value in the price of the checkout in front-office and show it in the back-office details so, my question is: what is the hook or the hooks to i will need to echieve this? Link to comment Share on other sites More sharing options...
knacky Posted April 6, 2022 Share Posted April 6, 2022 (edited) 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 April 6, 2022 by knacky (see edit history) Link to comment Share on other sites More sharing options...
Nicolas Pistillo Posted April 6, 2022 Author Share Posted April 6, 2022 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: Link to comment Share on other sites More sharing options...
knacky Posted April 6, 2022 Share Posted April 6, 2022 (edited) 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 April 6, 2022 by knacky (see edit history) Link to comment Share on other sites More sharing options...
knacky Posted April 6, 2022 Share Posted April 6, 2022 (edited) 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 April 6, 2022 by knacky (see edit history) 1 Link to comment Share on other sites More sharing options...
Nicolas Pistillo Posted April 6, 2022 Author Share Posted April 6, 2022 Thanks so much! this works in 1.7.5.2? that is the version to i use Link to comment Share on other sites More sharing options...
knacky Posted April 6, 2022 Share Posted April 6, 2022 I gave you a demonstration of how to do it, I didn't give you a proven working solution. Link to comment Share on other sites More sharing options...
knacky Posted April 6, 2022 Share Posted April 6, 2022 Tomorrow I'll upload a sample module on how to do it with your own hook. 1 Link to comment Share on other sites More sharing options...
Nicolas Pistillo Posted April 6, 2022 Author Share Posted April 6, 2022 Thanks again, knacky As you know, im not an expert with Prestashop, so all collaboration is welcome and grateful! See you tomorrow! 1 Link to comment Share on other sites More sharing options...
knacky Posted April 7, 2022 Share Posted April 7, 2022 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 More sharing options...
Nicolas Pistillo Posted April 7, 2022 Author Share Posted April 7, 2022 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 More sharing options...
knacky Posted April 7, 2022 Share Posted April 7, 2022 To get it right. VAT is 1.5% = 1.80 The total price without VAT is 120 The total price including VAT is 121.80 The total to pay is how much will it pay ???? 120 or 121.80 ??? Link to comment Share on other sites More sharing options...
Nicolas Pistillo Posted April 7, 2022 Author Share Posted April 7, 2022 In that example, the price to pay es 121.80, asumming the client has a VAT available for apply. Not all the clients have a VAT for apply to the checkout price. but in the case of it has, there is the logic Im explained well? I have a A2 english level haha Link to comment Share on other sites More sharing options...
knacky Posted April 7, 2022 Share Posted April 7, 2022 And the client without VAT pays 120 😄 Link to comment Share on other sites More sharing options...
Nicolas Pistillo Posted April 7, 2022 Author Share Posted April 7, 2022 Yes, exactly knacky Link to comment Share on other sites More sharing options...
knacky Posted April 7, 2022 Share Posted April 7, 2022 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 More sharing options...
knacky Posted April 7, 2022 Share Posted April 7, 2022 (edited) /* cart-summary-totals.tpl */ /* cart-detailed-totals.tpl */ 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 April 7, 2022 by knacky (see edit history) 1 Link to comment Share on other sites More sharing options...
Nicolas Pistillo Posted April 8, 2022 Author Share Posted April 8, 2022 Your are a god, knacky I currently work over that and learn with your code, thanks SO VERY much for yout help, 1 Link to comment Share on other sites More sharing options...
knacky Posted April 8, 2022 Share Posted April 8, 2022 This is an example of the simplest variant, but it has its pros and cons. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now