I want to Show Static CMS block in Magento 2 html file.
1 Answer
Replace your di.xml with below code :
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\CompositeConfigProvider">
<arguments>
<argument name="configProviders" xsi:type="array">
<item name="reg_custom_checkout_block" xsi:type="object">Reg\Checkout\Model\ConfigProvider</item>
</argument>
</arguments>
</type>
</config>
Update your getConfig() function from this below code :
<?php
namespace Reg\Checkout\Model;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\View\LayoutInterface;
class ConfigProvider implements ConfigProviderInterface
{
/** @var LayoutInterface */
protected $_layout;
protected $cmsBlock;
public function __construct(LayoutInterface $layout, $blockId)
{
$this->_layout = $layout;
$this->cmsBlock = $this->constructBlock($blockId);
}
public function constructBlock($blockId){
$block = $this->_layout->createBlock('Magento\Cms\Block\Block')
->setBlockId($blockId)->toHtml();
return $block;
}
public function getConfig()
{
$config = [];
$config['myCustomData'] = $this->constructBlock('your-block-id');
return $config;
}
}
inside knockout js
var customData = window.checkoutConfig.myCustomData;
console.log(customData);
-
I have updated both files di.xml and Model file but doesn't get success. Where to put knockout.js part as such not created ant file @Rohan– PritsJul 31, 2020 at 7:32
-
as you mention js file in question. you can use this inside js file. Jul 31, 2020 at 7:38
-
will i add this in cms-block.js file or any other file will you please help because it show my block in my step three or anything i am not getting .@Rohan– PritsJul 31, 2020 at 7:41
-
-
I have added that under initialize function in cms-block.js. but it shows undefined customData in console.@Rohan– PritsJul 31, 2020 at 7:51