0

I have an order model. How can I get the locale from the store view where the order was created in?

Edit

On some tutorials I found something like

/** @var \Magento\Sales\Model\Order $order */
$order->getStore()->getLocaleCode();

But this does not work (anymore).

2
  • Can you please attach a photo or more details regarding your question? Feb 28, 2023 at 9:20
  • This was a general question. But I already have found a solution (see below). Thank you anyway.
    – stollr
    Feb 28, 2023 at 10:57

1 Answer 1

0

The locale of a store is defined in the store config with the path 'general/locale/code'. To get the value of the correct store, one has to use the store ID of the order.

Here's an example how to achieve this in a helper:

namespace Acme\MyModule\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
    public function getOrderStoreLanguage(OrderInterface $order): ?string
    {
        $locale = $this->scopeConfig->getValue(
            'general/locale/code',
            ScopeInterface::SCOPE_STORE,
            $order->getStoreId()
        );

        if (!$locale) {
            return null;
        }

        return $locale;
    }
}

If you need that in another custom class, you have to inject the scope config into the constructor.

namespace Acme\MyModule\Observer;

use Magento\Framework\App\Config\ScopeConfigInterface;

class CustomObserver
{
    private ScopeConfigInterface $scopeConfig;

    public function __construct(ScopeConfigInterface $scopeConfig) {
        $this->scopeConfig = $scopeConfig;
    }
}

Then you can get the locale of the store in the same way as in the example above.

I hope this helps other's, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.