Skip to content

Instantly share code, notes, and snippets.

@cornobils
Created April 6, 2018 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cornobils/3472b94fd827e15d4e14159265a34549 to your computer and use it in GitHub Desktop.
Save cornobils/3472b94fd827e15d4e14159265a34549 to your computer and use it in GitHub Desktop.
[Sylius] getting shippingmethod tax category amount for checkout stage
{% for choice_form in form.method %}
{% set taxRate = form.method.vars.data|resolve_tax_rate %}
{% set fee = form.method.vars.shipping_costs[choice_form.vars.value] %}
{% set fee = fee + fee|calculate(taxRate) %}
{% set method = form.method.vars.choices[loop.index0].data %}
{% include '@SyliusShop/Checkout/SelectShipping/_choice.html.twig' with {'form': choice_form, 'method': method, 'fee': fee} %}
{% else %}
{% include '@SyliusShop/Checkout/SelectShipping/_unavailable.html.twig' %}
{% endfor %}
<?php
namespace AppBundle\Twig\Extension;
use Sylius\Component\Taxation\Calculator\DefaultCalculator;
use Sylius\Component\Taxation\Model\TaxRateInterface;
class DefaultCalculatorExtension extends \Twig_Extension
{
private $defaultCalculator;
public function __construct(DefaultCalculator $calculator)
{
$this->defaultCalculator = $calculator;
}
public function getFilters(): array
{
return [
new \Twig_Filter('calculate', [$this, 'calculate']),
];
}
/**
* {@inheritdoc}
*/
public function calculate(int $base, ?TaxRateInterface $taxRate)
{
if (!$taxRate) {
return 0;
}
return $this->defaultCalculator->calculate($base, $taxRate);
}
}
<?php
namespace AppBundle\Twig\Extension;
use Sylius\Component\Taxation\Model\TaxableInterface;
use Sylius\Component\Taxation\Model\TaxRateInterface;
use Sylius\Component\Taxation\Resolver\TaxRateResolver;
class TaxRateResolverExtension extends \Twig_Extension
{
private $taxRateResolver;
public function __construct(TaxRateResolver $taxRateResolver)
{
$this->taxRateResolver = $taxRateResolver;
}
public function getFilters(): array
{
return [
new \Twig_Filter('resolve_tax_rate', [$this, 'resolve']),
];
}
public function resolve(TaxableInterface $taxable): ?TaxRateInterface
{
return $this->taxRateResolver->resolve($taxable);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'tax_rate_resolver';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment