0

With the order id, how to retrieve the language code from the order or from the user ? (anything that looks remotely like EN, ES, FR, or en_UK, fr_FR).

For example : if the user has been on a website based on Magento 2.x , he browses the website in Russian, and he buys a chocolate bar Wonka Bar. His order id is 13371337.

How could one get "ru_RU" later on (the user is not logged in anymore) programatically ?

3
  • is Russian a store in your website?
    – Piyush
    Feb 28, 2018 at 12:43
  • Yes, it is a language from a store in my website
    – Cedric
    Mar 2, 2018 at 8:35
  • Then I guess getting store id from order and then language code from store will work for you
    – Piyush
    Mar 2, 2018 at 17:24

2 Answers 2

1

You can use this below code it will work based on customer IP placed but you need to create event sales order after(checkout_onepage_controller_success_action) and need to create the table in sales_order table.

<?php
namespace Namespace\Modulename\Observer;
use Magento\Sales\Model\Order;

class classname implements \Magento\Framework\Event\ObserverInterface
{

    public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager,
    \Magento\Framework\HTTP\Client\Curl $curl) {
        $this->_curl = $curl;
        $this->objectManager = $objectManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    { 
          $orderId = $observer->getEvent()->getOrderIds();
          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId[0]);
          $order->setLanguageCode($this->getCountryName())
          $order->save();
          return true;    
    }

    public function getCountryName() {
        $visitorIp = $this->getVisitorIp();
        $url = "freegeoip.net/json/".$visitorIp;
        $this->_curl->get($url);
        $response = json_decode($this->_curl->getBody(), true);
        $country_code = $response['country_code'];
        return $country_code;        
    }

    public function getVisitorIp() {       
        $remoteAddress = $this->objectManager->create('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');
        return $remoteAddress->getRemoteAddress();
    }
}
6
  • Great. Getting a language by geolocalisation will work 95% of the time, but sometime the users are browsing in another language, like a German living in England for instance.
    – Cedric
    Feb 28, 2018 at 13:15
  • Using Google Translate? Feb 28, 2018 at 13:31
  • How many languages stores do you have on your site? Feb 28, 2018 at 13:34
  • If this answer helpful for you means please accept the answer, It'll help others too. Feb 28, 2018 at 13:38
  • I haven't used it in the end, but it may be the most appropriate answer for the stackexchange users who will land here.
    – Cedric
    Mar 6, 2018 at 8:49
0
\Magento\Framework\App\ObjectManager::getInstance()
                        ->get('Magento\Framework\App\Config\ScopeConfigInterface')
                        ->getValue('general/locale/code', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $order->getStore()->getStoreId())

This will get the store's language that has been used for the $order

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.