Magento 2 Store Name – Get Store Name from Quote Object

magento2multistorequotestore-view

I have a custom module that works with the default Magento quote factory. Here, I am fetching the quote data using the,

\Magento\Quote\Model\QuoteFactory $quoteFactory

and in my block I am getting the store name as

public function getStoreName() {
    return $this->_storeManager->getStore()->getName();
}

However, when there are multiple store views, the above code always displays the store as Default Store View.
Is there anyway we can get the store name according to the quote object that was created?

Please can anyone help?

Best Answer

You can fetch your store id from your quote object and after that from id you can fetch your store name from your store id.

Please see my code.

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();    
$storeManager = $objectManager->create("\Magento\Store\Model\StoreManagerInterface");
$storeId = $quote->getStoreId();
    $stores = $storeManager->getStores(true, false);
    foreach($stores as $store){
    if($store->getId() === $storeId){
        $storeName = $store->getName();
    }
   }
echo $storeName;
Related Topic