2016-07-26 3 views
1

私は店の電話番号を表示するためにブロックを使用しようと、このエラーを取得しています:Magentoの2 - ディスプレイの電話番号ブロック誤り

<?php 

namespace MyVendor\Custom\Block; 

use Magento\Framework\View\Element\Template; 

class Phone extends Template 
{ 
    protected $_storeInfo; 

    public function __construct(
     \Magento\Framework\View\Element\Template\Context $context, 
     \Magento\Store\Model\Information $storeInfo, 
     array $data = [] 
    ) { 
     parent::__construct($context, $data); 
     $this->_storeInfo = $storeInfo; 
    } 

    public function getPhoneNumber() 
    { 
     return $this->_storeInfo->getStoreInformationObject()->getPhone(); 
    } 
} 

答えて

1

hollahollaのbruva:

Recoverable Error: Argument 1 passed to Magento\Store\Model\Information::getStoreInformationObject() must be an instance of Magento\Store\Model\Store, none given

は、ここに私のコードです

私は同じ問題を抱えていたし、私何からソリューション Here

を見つけましたgetStoreInformationObject関数の引数としてストアオブジェクトを設定する必要があります。これを行うには、

\Magento\Store\Model\StoreManagerInterface 

モデルをコンストラクタに挿入します。

次はこの

$this->_storeManagerInterface = $storeManagerInterface; 

のための変数を作成し、getStoreInformationObject()関数の引数としてgetStore()関数を渡します。

$this->_storeInfo->getStoreInformationObject($this->_storeManagerInterface->getStore())->getPhone(); 

あなたの最終的なコードは次のようになります。私も、これはエラーをスローしますと値が空でないことを確認するために、いくつかの検証を追加

class Helloworld 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() 
     { 
       $telephone = $this->_storeInfo->getStoreInformationObject($this->_storeManagerInterface->getStore())->getPhone(); 
       return (!empty($telephone)) ? $telephone : '' ; 
     } 
    } 

私はこれを手伝いました。 幸運