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