3. Configuring taxation

Danger

We’re sorry but this documentation section is outdated. Please have that in mind when trying to use it. You can help us making documentation up to date via Sylius Github. Thank you!

To start calculating taxes, we need to configure the system. In most cases, the configuration process is done via web interface, but you can also do it programatically.

3.1. Creating the tax categories

First step is to create a new tax category.

<?php

public function configureAction()
{
    $taxCategoryFactory = $this->container->get('sylius.factory.tax_category');
    $taxCategoryManager = $this->container->get('sylius.manager.tax_category');

    $clothingTaxCategory = $taxCategoryFactory->createNew();
    $clothingTaxCategory->setName('Clothing');
    $clothingTaxCategory->setDescription('T-Shirts and this kind of stuff.');

    $foodTaxCategory = $taxCategoryFactory->createNew();
    $foodTaxCategory->setName('Food');
    $foodTaxCategory->setDescription('Yummy!');

    $taxCategoryManager->persist($clothingTaxCategory);
    $taxCategoryManager->persist($foodTaxCategory);

    $taxCategoryManager->flush();
}

3.2. Categorizing the taxables

Second thing to do is to classify the taxables, in our example we’ll get two products and assign the proper categories to them.

<?php

public function configureAction()
{
    $tshirtProduct = // ...
    $bananaProduct = // ... Some logic behind loading entities.

    $taxCategoryRepository = $this->container->get('sylius.repository.tax_category');

    $clothingTaxCategory = $taxCategoryRepository->findOneBy(['name' => 'Clothing']);
    $foodTaxCategory = $taxCategoryRepository->findOneBy(['name' => 'Food']);

    $tshirtProduct->setTaxCategory($clothingTaxCategory);
    $bananaProduct->setTaxCategory($foodTaxCategory);

    // Save the product entities.
}

3.3. Configuring the tax rates

Finally, you have to create appropriate tax rates for each of categories.

<?php

public function configureAction()
{
    $taxCategoryRepository = $this->container->get('sylius.repository.tax_category');

    $clothingTaxCategory = $taxCategoryRepository->findOneBy(['name' => 'Clothing']);
    $foodTaxCategory = $taxCategoryRepository->findOneBy(['name' => 'Food']);

    $taxRateFactory = $this->container->get('sylius.factory.tax_rate');
    $taxRateManager = $this->container->get('sylius.repository.tax_rate');

    $clothingTaxRate = $taxRateFactory->createNew();
    $clothingTaxRate->setCategory($clothingTaxCategory);
    $clothingTaxRate->setName('Clothing Tax');
    $clothingTaxRate->setCode('CT');
    $clothingTaxRate->setAmount(0.08);
    $clothingTaxRate->setCalculator('default');

    $foodTaxRate = $taxRateFactory->createNew();
    $foodTaxRate->setCategory($foodTaxCategory);
    $foodTaxRate->setName('Food');
    $foodTaxRate->setCode('F');
    $foodTaxRate->setAmount(0.12);
    $foodTaxRate->setCalculator('default');

    $taxRateManager->persist($clothingTaxRate);
    $taxRateManager->persist($foodTaxRate);

    $taxRateManager->flush();
}

Done! See the “Calculating Taxes” chapter to see how to apply these rates.