Jump to content

PS 1.6 Create tax included / tax excluded switch button


Recommended Posts

Hello,

 

Just as in the title, I would like to add a module that will look like a language switch but will change the way prices are described. Simply to set all visible prices to tax included or without tax when pressed tax excluded. This would need to be saved in a cookie. Prices should only be displayed as with or without tax they should always be calculated with tax.

 

Does the prestashop support this already or a new module needs to be created?

 

I would appreciate any tip, many thanks.

Share this post


Link to post
Share on other sites

  • 8 months later...

Anyone could help on this?

 

How to save and change a session cookie? 

	{if $includeTax}
	  <div class="tax">
		<a href="" onclick="changeTax()" <span> Tax included</span></a>
	  </div>
	{else}
	  <div class="tax">
		<a href="" onclick="changeTax()" <span> Tax excluded </span></a>
	  </div>
	{/if}

Share this post


Link to post
Share on other sites

the cookie isn't the issue. you need a module together with an override since a simple override wouldn't be enough.

 

the default tax calculation is controlled by tax rules and taxmanager. there you'll need to hook up.

Share this post


Link to post
Share on other sites

I think i made it

 

 

In TPL

<script>
{literal}
    function setTaxExclude() {
        var expire = new Date();
        expire.setMonth(expire.getMonth() + 12);
        document.cookie = 'taxDisplay'+ "=" + 'include' + ";path=/;" + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
    }
	
	function setTaxInclude() {
	var expire = new Date();
	expire.setMonth(expire.getMonth() + 12);
	document.cookie = 'taxDisplay' + "=" + 'exclude' + ";path=/;" + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
    }

{/literal}
</script>

	{if $tax == 'include'}
	<div class="TaxDisplay">
		<a href="" onclick="setTaxExclude()" <span> Tax included</span></a>
	</div>
	{else}
		<div class="TaxDisplay">
		<a href="" onclick="setTaxInclude()" <span> Tax excluded</span></a>
	</div>
	{/if}

in php

 $this->smarty->assign(array('tax' => $_COOKIE['tax']));

This is a button to change the var (include/exclude). Then just use {if $tax == 'include'} show price with tax in products.tpl and others...

 

It worked for me but as I am newbie idk about performance...

Edited by GabrielMota (see edit history)

Share this post


Link to post
Share on other sites

In which tpl did you add this code?

 

Some remarks:

-> prestashop has its own cookie function which I would use.

-> this is only about setting a cookie. it does nothing with overriding the price display.

-> prestashop uses several rules which control price display and calculations.

-> the challenge is to override only display but not calculation.

Share this post


Link to post
Share on other sites

I added in a tpl of module "blockuserinfo" so the button appear at the top.

 

By replacing "public static function getPriceDisplayMethod($id_group)" function code in Group.php with

if($_COOKIE['taxDisplay'] == 0) 
return 1;
else return 0;

Will make the product display the price with/without tax.

Edited by GabrielMota (see edit history)

Share this post


Link to post
Share on other sites

I've found it. What I see is that the prices change when in the product details page or in a categoy page.

However the prices do not change, when I am on the home page.

It might be a caching issue.

 

For example: homefeatured as many other modules write data to a cached file once a page fragment has been built.

This helps in terms of performance.

 

The culprit now could be that the cache information is tandomly populated with prices incl. or prices excl. depending on what user last called the page when it was stale or uncached.

Share this post


Link to post
Share on other sites

You said:

 

in php
 
$this->smarty->assign(array('tax' => $_COOKIE['tax']));
 
but where exactly in php? I am trying to create override for Group.php
 
<?php
class Group extends GroupCore
{
public static function getPriceDisplayMethod($id_group)
    {
		$this->smarty->assign(array('tax' => $_COOKIE['tax']));
		if($_COOKIE['taxDisplay'] == 0) 
			return 1;
		else return 0;
    }
}

but it gives internal server error.

 

Where should I add that php code?

 

Thanks

Share this post


Link to post
Share on other sites

In a php to pass the smarty to the tpl. Eg: by adding in blockuserinfo.php module in HookDisplayTop function you coud then use the var $tax in blockuserinfo.tpl to switch the text.

Don't assign the smarty in Group.php

 

 

For Homepage featured modules need to either disable the cache to those modules(via code) or duplicate the caches (with/without tax) by duplicating the tpl in module folder and adding in module php the code with the duplicated tpl

if (!$this->isCached('homefeatured.tpl', $this->getCacheId('homefeatured.tpl')) || !$this->isCached('homefeatured.tpl', $this->getCacheId('homefeaturedtax.tpl')))
{
..... module code updating cache
}

if($_COOKIE['taxDisplay'] == 0) 
     return $this->display(__FILE__, 'homefeatured.tpl', $this->getCacheId('homefeatured.tpl'));
else return $this->display(__FILE__, 'homefeatured.tpl', $this->getCacheId('homefeaturedtax.tpl'));
Edited by GabrielMota (see edit history)

Share this post


Link to post
Share on other sites

  • 3 years 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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More