0

I have used below code in magento1 to load the static block with custom variables.

Here is the code of my m1 Block file.

 {
 $customer = Mage::getSingleton('customer/session')->getCustomer();
 $block = Mage::getModel('cms/block')
 ->setStoreId(Mage::app()->getStore()->getId())
 ->load('static_block_id');
 if($block->getIsActive()) {
 $array = array();
 $array['custom_value1'] = $formattedPrice = Mage::helper('core')->currency($customer->getNoofOrders(), true, false);
 $array['custom_value2'] = $formattedPrice = Mage::helper('core')->currency($customer->getSavingsToDate(), true, false);
 $array['registration_date'] = date('F Y', strtotime($customer->getRegistrationDate()));
 $filter = Mage::getModel('cms/template_filter');
 $filter->setVariables($array);
 // return the filtered block content.
 return $filter->filter($block->getContent());
}

Here getNoofOrders and getSavingsToDate are the custom customer attributes.

How can i use the same code in magento 2 format. If so please anybody help me with this how to set the custom variables and that is used in static blocks. Thanks

asked Nov 2, 2018 at 7:12
2
  • Please check my answer. Commented Nov 2, 2018 at 8:49
  • Is it working ? Commented Nov 2, 2018 at 9:26

1 Answer 1

2

Try to use this below code :

protected $_customerSession;
protected $_blockFactory;
protected $_priceFormat;
protected $_filterProvider;
protected $storeManager;
public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Customer\Model\Session $customerSession,
 \Magento\Cms\Model\BlockFactory $blockFactory,
 \Magento\Framework\Pricing\Helper\Data $priceFormat,
 \Magento\Cms\Model\Template\FilterProvider $filterProvider,
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 array $data = []
) {
 parent::__construct($context, $data);
 $this->_customerSession = $customerSession;
 $this->_blockFactory = $blockFactory;
 $this->_priceFormat = $priceFormat;
 $this->_filterProvider = $filterProvider;
 $this->storeManager = $storeManager;
}
public function yourFunction()
{
 $customer = $this->_customerSession->getCustomer();
 $block = $this->_blockFactory->create()->setStoreId($this->storeManager->getStore()->getId())->load('static_block_id');
 if($block->getIsActive()) {
 $array = [];
 $array['custom_value1'] = $formattedPrice = $this->_priceFormat->currency($customer->getNoofOrders(), true, false);
 $array['custom_value2'] = $formattedPrice = $this->_priceFormat->currency($customer->getSavingsToDate(), true, false);
 $array['registration_date'] = date('F Y', strtotime($customer->getRegistrationDate()));
 $filter = $this->filterProvider->getBlockFilter()->setStoreId($this->storeManager->getStore()->getId())->setVariables($array)->filter($block->getContent());
 return $filter;
 }
}

Hope, It may be helpful for you.

answered Nov 2, 2018 at 8:49
1
  • Happy to help !! Happy coding :) Commented Nov 2, 2018 at 9:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.