Je veux afficher le numéro de téléphone enregistré dans l'admin magento dans frontend dans magento 2.
Comme dans Magento 1.9, c'est comme
$storePhone = Mage::getStoreConfig('general/store_information/phone');
Je veux afficher le numéro de téléphone enregistré dans l'admin magento dans frontend dans magento 2.
Comme dans Magento 1.9, c'est comme
$storePhone = Mage::getStoreConfig('general/store_information/phone');
Réponses:
Vous devrez utiliser la Magento/Store/Model/Informationclasse et appeler la getStoreInformationObject()méthode pour cela.
Vous devrez cependant injecter cette classe dans votre bloc personnalisé pour pouvoir l'utiliser dans votre modèle.
protected $_storeInfo;
public function __construct(
    ....
    \Magento\Store\Model\Information $storeInfo,
    ....
) {
    ...
    $this->_storeInfo = $storeInfo;
    ....
}
Créez ensuite une méthode personnalisée pour récupérer le numéro de téléphone:
public function getPhoneNumber()
{
    return $this->_storeInfo->getStoreInformationObject(Store $store)->getPhone();
}
Ainsi, dans votre modèle, vous pouvez appeler:
$block->getPhoneNumber();
Vous ne devez jamais utiliser le gestionnaire d'objets directement (voir pourquoi ici: Magento 2: utiliser ou ne pas utiliser directement ObjectManager? )
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $objectManager->create('Magento/Store/Model/Information');
$storeInfo = $storeInformation->getStoreInformationObject($store);
Ensuite, vous pouvez obtenir le téléphone en appelant:
$phone = $storeInfo->getPhone();
              $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $objectManager->create('Magento\Store\Model\Information');
$store = $objectManager->create('Magento\Store\Model\Store');
$storeInfo = $storeInformation->getStoreInformationObject($store);
$phone = $storeInfo->getPhone();
              vous devez injecter l'instance an \Magento\Framework\App\Config\ScopeConfigInterfacedans votre bloc.
$protected $scopeConfig;
public function __construct(
    ....
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    ....
) {
    ...
    $this->scopeConfig = $scopeConfig;
    ....
}
Créez ensuite la méthode getStorePhone()
public function getStorePhone()
{
    return $this->scopeConfig->getValue(
        'general/store_information/phone',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}
et appelez dans votre modèle echo $block->getStorePhone()
Les méthodes ci-dessus ne fonctionnaient pas, j'ai donc essayé de la manière suivante et cela fonctionne pour moi ...
namespace Vendor\Module\Block;
class Contact extends \Magento\Framework\View\Element\Template
{
    protected $_storeInfo;
    protected $_storeManagerInterface;
    public function __construct( 
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\Information $storeInfo,
        \Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
        array $data = []
    )
    {
        parent::__construct($context, $data); 
        $this->_storeInfo = $storeInfo;
        $this->_storeManagerInterface = $storeManagerInterface;
    }
    public function getPhoneNumber()
    {
        return $this->_storeInfo->getStoreInformationObject($this->_storeManagerInterface->getStore(null))->getPhone();
    }
}
et dans le fichier modèle que j'ai appelé
echo $block->getPhoneNumber();
              Le code ci-dessus ne fonctionne pas pour moi. J'ai essayé le code suivant qui fonctionne.
class Sociallinks extends \Magento\Framework\View\Element\Template
{
   protected $socialLinksHelper;
   protected $objMgr;
   protected $storeInfo;
   protected $scopeConfig;
   public function __construct(
      \Magento\Framework\View\Element\Template\Context $context,
      \Addpeople\Websettings\Helper\Data $myModuleHelper,
      array $data = []
    ) {
    parent::__construct($context, $data);
    $this->_socialLinksHelper = $myModuleHelper;
    $this->_objMgr =  \Magento\Framework\App\ObjectManager::getInstance();
    $storeInformation = $this->_objMgr->create('Magento\Store\Model\Information');
    $store = $this->_objMgr->create('Magento\Store\Model\Store');
    $this->_storeInfo = $storeInformation->getStoreInformationObject($store);
}
public function getPhoneNumber()
{
    return $this->_storeInfo->getPhone();
}
}
Fichier modèle
<?php echo $block->getPhoneNumber();?>
Nous pouvons également utiliser:
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$storePhone = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('general/store_information/phone');