Created
April 6, 2018 11:45
-
-
Save chornobils/3472b94fd827e15d4e14159265a34549 to your computer and use it in GitHub Desktop.
[Sylius] getting shippingmethod tax category amount for checkout stage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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