আমি ম্যাজেন্টো 2-এর সম্মুখভাগে ম্যাজেন্টো অ্যাডমিনে সংরক্ষিত ফোন নম্বরটি প্রদর্শন করতে চাই।
ম্যাজেন্টো ১.৯ এ লাইক করুন like
$storePhone = Mage::getStoreConfig('general/store_information/phone');
আমি ম্যাজেন্টো 2-এর সম্মুখভাগে ম্যাজেন্টো অ্যাডমিনে সংরক্ষিত ফোন নম্বরটি প্রদর্শন করতে চাই।
ম্যাজেন্টো ১.৯ এ লাইক করুন like
$storePhone = Mage::getStoreConfig('general/store_information/phone');
উত্তর:
আপনাকে Magento/Store/Model/Informationক্লাসটি ব্যবহার করতে হবে এবং তার getStoreInformationObject()জন্য পদ্ধতিটি কল করতে হবে।
যদিও আপনার টেমপ্লেটে যদিও এটি ব্যবহার করতে সক্ষম হতে আপনাকে এই ক্লাসটি আপনার কাস্টম ব্লকে ইনজেক্ট করতে হবে।
protected $_storeInfo;
public function __construct(
....
\Magento\Store\Model\Information $storeInfo,
....
) {
...
$this->_storeInfo = $storeInfo;
....
}
তারপরে ফোন নম্বরটি পুনরুদ্ধার করতে একটি কাস্টম পদ্ধতি তৈরি করুন:
public function getPhoneNumber()
{
return $this->_storeInfo->getStoreInformationObject(Store $store)->getPhone();
}
সুতরাং আপনার টেমপ্লেটে আপনি কল করতে পারেন:
$block->getPhoneNumber();
আপনার কখনই সরাসরি অবজেক্ট ম্যানেজারটি ব্যবহার করা উচিত নয় (দেখুন কেন এখানে: ম্যাজেন্টো 2: সরাসরি অবজেক্টম্যানেজারটি ব্যবহার করতে বা ব্যবহার করার জন্য নয়? )
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $objectManager->create('Magento/Store/Model/Information');
$storeInfo = $storeInformation->getStoreInformationObject($store);
তারপরে আপনি ফোন করে ফোনটি পেতে পারেন:
$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();
আপনাকে \Magento\Framework\App\Config\ScopeConfigInterfaceআপনার ব্লকের একটি উদাহরণ ইনজেক্ট করতে হবে ।
$protected $scopeConfig;
public function __construct(
....
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
....
) {
...
$this->scopeConfig = $scopeConfig;
....
}
তারপরে মেথডটি তৈরি করুন getStorePhone()
public function getStorePhone()
{
return $this->scopeConfig->getValue(
'general/store_information/phone',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
এবং আপনার টেম্পলেট কল echo $block->getStorePhone()
উপরের পদ্ধতিগুলি কাজ করে না তাই আমি নিম্নলিখিত পদ্ধতিতে চেষ্টা করেছি এবং এটি আমার জন্য কাজ করছে ...
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();
}
}
এবং টেমপ্লেট ফাইল আমি কল করেছি
echo $block->getPhoneNumber();
উপরের কোডটি আমার পক্ষে কাজ করছে না। আমি নিম্নলিখিত কোডটি চেষ্টা করে যা কাজ করে।
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();
}
}
টেমপ্লেট ফাইল
<?php echo $block->getPhoneNumber();?>
আমরা এটিও ব্যবহার করতে পারি:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storePhone = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('general/store_information/phone');