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')

    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');

        // 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


1 Answer 1


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.

  • Happy to help !! Happy coding :) Nov 2, 2018 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.