Logo BrocksiNet

BrocksiNet

Try to clean the web

PHP-Snippet: How to get the defaultTaxRate in Shopware 6?

Sometimes it is maybe useful to get the defaultTaxRate that is defined via Admin in Shopware 6. For this we have this small php function below to get the UUID from the SystemConfig and then search with the tax.repository to get the curent value. The system path to get the default UUID is core.tax.defaultTaxRate.

Here is the PHP-Snippet for you:

private function getDefaultTaxFromConfig(Context $context): ?float
{
    $defaultTaxRateUuid = $this->systemConfig->get('core.tax.defaultTaxRate');
    $taxRateCriteria = new Criteria([$defaultTaxRateUuid]);
    $taxRateSearchResult = $this->taxRateRepository->search($taxRateCriteria, $context);
    if ($taxRateSearchResult->getTotal() > 0) {
        $tax = $taxRateSearchResult->first();
        if (!$tax instanceof TaxEntity) {
            throw new \InvalidArgumentException(
                sprintf('Tax rule with id %s not found taxId missing', $defaultTaxRateUuid)
            );
        }

        $productTaxRate = $tax->getTaxRate();
    }

    return $productTaxRate ?? null;
}

Maybe someone needs this at some point 🤔

Released - 23.11.2022

Comments


About the author

Bild Bjoern
Björn MeyerSoftware Engineer
Björn is interested in the details of web technologies. He started with Joomla, Drupal, Typo3, vBulletin, Fireworks and Photoshop. Today his focus is on PHP for example with Magento 2, Shopware 6 and Symfony. He also likes JavaScript with Angular, React, VueJs and TypeScript. In his free time, he seeks constant balance with swimming, running and yoga.