For developers
Interfaces for themes and other plugins.
Include the switch yourself
{% sw_include '@Storefront/storefront/component/taxswitch-switcher.twig' with {
style: 'link',
position: 'custom'
} %}
style is dropdown, segmented (default), toggle or link. position is used for CSS classes and as the tracking source. The switch is only rendered if it is active for the visitor.
Your own buttons
Every element with one of these attributes switches the mode, also in content that is loaded later:
<button type="button" data-taxswitch-set-net="CookieWarningModal">Net</button>
<button type="button" data-taxswitch-set-gross="CookieWarningModal">Gross</button>
<button type="button" data-taxswitch-set-toggle="CookieWarningModal">Toggle</button>
| Attribute or value | Effect without consent, or additional effect |
|---|---|
CookieWarningModal | opens the cookie hint of the plugin |
CookieConsentLayer | opens the Shopware cookie settings |
data-taxswitch-source="name" | source for tracking |
data-taxswitch-disable-reload="true" | no reload after switching |
data-taxswitch-redirect-url="…" | go to this address after switching |
JavaScript events
On every change two events fire before the reload:
document.addEventListener('taxswitch:change', (event) => {
const { from, to, source } = event.detail; // e.g. 'gross', 'net', 'header'
});
document.$emitter.subscribe('TaxSwitch_changed', (event) => console.log(event.detail));
Switch from a script:
document.$emitter.publish('TaxSwitch_setTaxToNet', {
noCookieBehavior: 'CookieWarningModal',
disableReload: false,
redirectUrl: null,
source: 'my-script'
});
Also available: TaxSwitch_setTaxToGross and TaxSwitch_setTaxToToggle.
Connect a cookie manager
To make the popup wait for the banner of another cookie manager, give the plugin its CSS selector. The banner counts as visible as long as the element is displayed:
<body data-devert-tax-switch-options='{"cookieBannerSelector": "#my-consent-banner"}'>
Or report the state yourself:
window.taxswitch = window.taxswitch || {};
window.taxswitch.cookieBannerOpen = true; // before the plugin starts
document.$emitter.publish('TaxSwitch_cookieBannerOpened');
document.$emitter.publish('TaxSwitch_cookieBannerClosed');
Change the tax mode with PHP
The event TaxDetectorDecoratorUseGrossEvent is dispatched after customer group and cookie. Your subscriber can change the result:
use Devert\TaxSwitch\Service\TaxDetectorDecoratorUseGrossEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ForceNetSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [TaxDetectorDecoratorUseGrossEvent::class => 'onUseGross'];
}
public function onUseGross(TaxDetectorDecoratorUseGrossEvent $event): void
{
if ($this->mustBeNet($event->getSalesChannelContext())) { // your condition
$event->getUseGrossStd()->showGross = false;
}
}
}
Orders created in the Administration and calls without a web request (for example when the sitemap is generated) do not dispatch the event.
Mind the HTTP cache: the plugin separates cached pages by the cookie tax_switch. If your condition depends on something else that is not part of the cache key, a visitor may see the prices of another mode.
Second price in your own templates
{% set secondary = taxswitch_secondary_price(product.calculatedPrice, product) %}
{% if secondary %}
{{ secondary.amount|currency }} ({{ secondary.mode }})
{% endif %}
The function returns amount, listAmount (list price or null) and mode (gross or net, describing the returned amount). For tax-free prices the result is empty.